首页 > 解决方案 > 如何在 linq 中添加多个 where 子句?

问题描述

我有这个查询

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 是其中的另一列,但我无法正确使用语法。有谁知道如何改变它?

谢谢

标签: c#linq

解决方案


将 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);

推荐阅读