How can you update a Linq Expression with additional parameters?

前端 未结 5 927
感情败类
感情败类 2020-12-29 14:28

I have a Linq Expression, which may be altered depending on certain conditions. An example of what I would like to do (left blank the bit I am not sure about):



        
5条回答
  •  失恋的感觉
    2020-12-29 14:36

    If you Get method retrives the data and returns in memory objects the you could do so

    Expression> filter = (Project p) => p.UserName == "Bob";
    if(showArchived) {
         filter = (Project p) => p.UserName == "Bob" && p.Archived;
    }
    IEnumerable projects = unitOfWork.ProjectRepository.Get(filter);
    

    EDIT

    Just to point out. When you use .ToList() method it enumerates the Queryable, i.e. makes a database request.

提交回复
热议问题