How do I implement a dynamic 'where' clause in LINQ?

后端 未结 7 2083
有刺的猬
有刺的猬 2020-12-29 09:57

I want to have a dynamic where condition.

In the following example:

var opportunites =  from opp in oppDC.Opportunities
                        


        
7条回答
  •  有刺的猬
    2020-12-29 10:34

    The WHERE clause could be done something like

    //...
    where string.IsNullOrEmpty(title) ? true : opp.Title.StartsWith(title)
    //...
    

    Dynamically returning records I don't think is possible in LINQ since it needs to be able to create a consistent AnonymousType (in the background)

提交回复
热议问题