我需要重构方法Test1和Test2。
AddInfo != null
:
public List<FileRequest> Test1(long fileId)
{
return context.FileRequest.Include("AddInfo")
.Where(x=>x.FileId = fileId && x.AddInfo != null)
}
AddInfo == null
:
public List<FileRequest> Test2(long fileId)
{
return context.FileRequest.Include("AddInfo")
.Where(x=>x.FileId = fileId && x.AddInfo == null)
}
我尝试创建generalize方法,但无法实现。
public List<FileRequest> Test2(long fileId, Expression<Func<AddInfo, bool>> expression)
{
return context.FileRequest.Include("AddInfo")
.Where(x=>x.FileId = fileId && expression)
}
我如何重构我的方法?
答案 0 :(得分:1)
最简单的选择是只使用两个Where
来电:
public IQueryable<FileRequest> Test2(long fileId,
Expression<Func<FileRequest, bool>> predicate)
{
return context.FileRequest.Include("AddInfo")
.Where(x=>x.FileId == fileId)
.Where(predicate);
}