LINQ在何处具有AND OR条件 [英] LINQ Where with AND OR condition

查看:78
本文介绍了LINQ在何处具有AND OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此我设法使该查询正常工作

So I have managed to get this query working

List<string> listStatus = new List<string>() ; 
listStatus.add("Text1");

List<string> listMerchants = new List<string>() ;
listMerchants.add("Text2");


from item in db.vw_Dropship_OrderItems
             where listStatus.Contains(item.StatusCode) 
                      && listMerchants.Contains(item.MerchantId)
             select item;

在这里,我想检查listStatus和listMerchants是否不为null,然后将它们放在WHERE子句中.

Here I would like to check if listStatus and listMerchants are not null only then put them inside WHERE clause.

喜欢

如果listMerchants为null,则查询将类似于

if listMerchants is null then query will be like

     where listStatus.Contains(item.StatusCode) 

我不想使用switch或If条件.

I do not want to use switch or If condition.

谢谢

推荐答案

from item in db.vw_Dropship_OrderItems
    where (listStatus != null ? listStatus.Contains(item.StatusCode) : true) &&
    (listMerchants != null ? listMerchants.Contains(item.MerchantId) : true)
    select item;

如果listMerchants和listStatus都为null,则可能会产生奇怪的行为.

Might give strange behavior if both listMerchants and listStatus are both null.

这篇关于LINQ在何处具有AND OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆