iT邦幫忙

0

.Net Core在不同Method存取EF時如何使用相同的Where 條件

直接舉例

private dynamic FetchDeptList(string orgCode)
{
    return _deptRepo.Read(x => x.OrgCode == orgCode 
               && (User.IsViewAll || User.AreaCodeList.Contains(x.AreaCode))
               && (User.IsViewAll || User.DeptCodeList.Contains(x.DeptCode))
               && x.Enable == true)
}

private dynamic FetchEmpList(string orgCode)
{
    return _empRepo.Read(x => x.OrgCode == orgCode 
               && (User.IsViewAll || User.AreaCodeList.Contains(x.AreaCode))
               && (User.IsViewAll || User.DeptCodeList.Contains(x.DeptCode))
               && x.Enable == true)
}

private dynamic FetchOfficeList(string orgCode)
{
    return _officeRepo.Read(x => x.OrgCode == orgCode 
               && (User.IsViewAll || User.AreaCodeList.Contains(x.AreaCode))
               && (User.IsViewAll || User.DeptCodeList.Contains(x.DeptCode))
               && x.Enable == true)
}

請問以上三個method 分別在不同的cs裡,但條件式又都一模一樣,有什麼方法可以簡化呢?

看更多先前的討論...收起先前的討論...
canrong iT邦新手 3 級 ‧ 2022-05-18 20:51:15 檢舉
用泛型拉出來共用
gmlin iT邦新手 5 級 ‧ 2022-05-19 14:12:59 檢舉
請問有範例或要下什麼keyword才能找到相關資料呢?
public Func<dynamic, bool> condition = (x) =>{ return x.OrgCode == orgCode
&& (User.IsViewAll || User.AreaCodeList.Contains(x.AreaCode))
&& (User.IsViewAll || User.DeptCodeList.Contains(x.DeptCode))
&& x.Enable == true; }

_Repo.Read(condition)
canrong iT邦新手 3 級 ‧ 2022-05-20 21:34:30 檢舉
https://ithelp.ithome.com.tw/questions/10208277 我在這篇回答類似寫法 可以照著這方向實作看看
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答