多列Lambda表达式

时间:2019-02-12 12:55:50

标签: c# linq lambda where-clause

在此处完成本教程后:
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

我目前使用以下

userAccesses = userAccesses.Where(s => s.employeeDetail.employeeNumber.ToUpper().Contains(searchValue.ToUpper()));

但是,我想将knownas /姓氏列连接起来,然后对所连接的项进行包含。

有人可以解释/建议一个示例语法吗?

这是我在下面尝试过的,但是我确定我的语法不正确。

userAccesses = userAccesses.Where(s => s.employeeDetail.employeeNumber + " " + s.employeeDetail.knownas + " " + s.employeeDetail.surname).Contains(searchValue);

感谢大家的回应,下面是最终的工作版本。

userAccesses.Where(x => (x.employeeDetail.employeeNumber + x.employeeDetail.knownas + x.employeeDetail.surname).Contains(searchValue));

3 个答案:

答案 0 :(得分:1)

您只需要连接字符串并在该字符串上调用Contains。

userAccesses
  .Where(s => $"{s.employeeDetail.employeeNumber} {s.employeeDetail.knownas} {s.employeeDetail.surname}".Contains(searchValue))

如果您需要一个可枚举的字符串作为表达式的结果,则还可以选择使用以下内容:

userAccesses
  .Select(s => $"{s.employeeDetail.employeeNumber} {s.employeeDetail.knownas} {s.employeeDetail.surname}")
  .Where(s => s.Contains(searchValue))

答案 1 :(得分:1)

您应该朝这个方向

public class Employee
{
    public string knownas { get; set; }

    public string userName { get; set; }
}
public void Test()
{
    List<Employee> employess = new List<Employee>();
    string searchvalue = "test";

    var listEmplyer = employess.Where(x => (x.userName + x.knownas).Contains(searchvalue));
}

答案 2 :(得分:0)

搜索串联的列值很奇怪...

我建议这样:

var filtered = userAccesses.Where(s => s.employeeDetail.employeeNumber.Contains(searchValue)
                                       || s.employeeDetail.knownas.Contains(searchValue)
                                       || s.employeeDetail.surname.Contains(searchValue));