如何在linq中添加多个where子句?

时间:2018-10-04 16:59:35

标签: c# linq

我有这个查询

db.v_Report_CompanySearches
    .Select(x => x.PersonName)  //Only return Names
    .Distinct()     // Make to Unique
    .Where(y => y.ToLower().Contains(mPersonName))  //where 
    .OrderBy(x => x);

我只希望它返回1列,称为PersonName,但我想将where子句更改为

PersonName.ToLower().Contains(mPersonName) || AccountName.ToLower().Contains(mPersonName)

AccountName是那里的另一列,但是我语法不正确。有人知道如何更改吗?

谢谢

2 个答案:

答案 0 :(得分:5)

将Where子句放在Select之前

db.v_Report_CompanySearches
.Where(y => y.PersonName.ToLower().Contains(mPersonName) || y.AccountName.ToLower().Contains(mPersonName) )  //where 
.Select(x => x.PersonName)  //Only return Names
.Distinct()     // Make to Unique
.OrderBy(z => z);

答案 1 :(得分:1)

您应该将Select移到Where之后:

db.v_Report_CompanySearches
.Where(y => y.PersonName.ToLower().Contains(mPersonName) 
            || y.AccountName.ToLower().Contains(mPersonName))  //where 
.Select(x => x.PersonName)  //Only return Names
.Distinct()     // Make to Unique
.OrderBy(x => x);