If condition in LINQ Where clause

╄→гoц情女王★ 提交于 2019-12-17 11:21:16

问题


Can I use if clause with Linq where?


回答1:


Yes you can like:

var query = someList.Where(a => a == "something");
if (condition)
{
    query = query.Where(b => b == "something else");
}
var result = query.ToList();

Because Where is producing an IQueryable, the execution is deferred until the ToList in my example so you can chain Wheres together as much as you want and then just execute it after you have passed all your conditions.




回答2:


var query = someList.Where(a => (someCondition)? a == "something" : true);

so, if 'someCondition' is false, 'Where' will be skipped.




回答3:


Make use of WhereIf extenstion method avaialbe in linq

Example

if (SearchControlMain.PostingID.HasValue) 
    query = query.Where(q => q.PostingID == SearchControlMain.PostingID);

instead of above go for the below

query = query.WhereIf(SearchControlMain.CategoryID.HasValue, q => q.CategoryID == SearchControlMain.CategoryID);

LINQ WhereIf Extension Method

LINQ to SQL Where Clause Optional Criteria




回答4:


Not sure if this is appropriate but it is quite useful, you can use ifs quite handily with conditional where clauses:

 var r = (from p in productinfo.tblproduct
                     where p.Accountid == accountid
                     select p);

            if (uuf1 != null)
                r = r.Where(p => p.UnitUserField1 == uuf1);

            if (uuf2!= null)
                r = r.Where(p => p.UnitUserField2 == uuf2);

So the where clause will be amended according to what is in UUF1 or UUF2 i.e. you might have only UUF1 with info, in which case it will take that and ignore the UUF2 where clause, you might have both in which it will take both or you might not have anything in UUF1 or 2 and your where clause will just take the accountid as the where clause.




回答5:


In my case there were two "conditional" where depending on search keys, so I did:

    var query = db.Package.Include("SomeThing")
    .Where(item => searchString1 == null || searchString1 == "" || item.Contains(searchString1))
    .Where(item => searchString2 == null || searchString2 == "" || item.Contains(searchString2));
    ...



回答6:


I had a scenario like this where I had to check for null within the list itself. This is what I did.

items = from p in items
        where p.property1 != null   //Add other if conditions
        select p;

// Use items the way you would use inside the if condition

But as Kelsey pointed out this would work too -

items = items.Where(a => a.property1 != null);



回答7:


I'm not sure what the question is, but a possible answer could be:

Yes,

list.Where(item => { if (Foo(item)) return true; else return false; });

It would be a complicated way of saying something simple, though.




回答8:


from item in items
where condition1
&& (condition2 ? true : condition3)
select item

This is how can you can do it with the noob Linq syntax. This applies the condition3 only if condition2 is false. If condition2 is true, you are essentially doing && true which has no effect on the where clause.

So it is essentially doing this:

if(condition2)
{
    from item in items
    where condition1
    select item
else
{
    from item in items
    where condition1
    && condition3
    select item
}


来源:https://stackoverflow.com/questions/3682835/if-condition-in-linq-where-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!