首页 > 解决方案 > LINQ 连接谓词 OR

问题描述

嗨我正在尝试连接一个 linq 表达式 例如:我有一个 List<string[]>需要在循环中读取这个的地方我需要创建一个这样的查询

from table where (name ='someone' && id='123') || (name ='another one' && id='223') || ( name='noone' && id='456')

以下代码是我正在处理的

foreach (var item in data)
{
    var name= item[4];
    var cnpj = item[1];
    Expression<Func<IncidentIntegration, bool>> predicated = (x => (x.EmployeesBase.name== name && x.Branch.id== id));
    query = query.Union(query.Where(predicated));
 }

但它正在创建这样的查询

from table where (name ='someone' || name ='another one' || name='noone') && ( id='223' || id='123' || id='456')

有什么办法可以连接这个吗?

标签: c#linqlambdapredicate

解决方案


我想它可以帮助你

如果我们将 CompleteInfos 视为您的表格,那么:

public class CompleteInfos
{
    public int Id { get; set; }
    public string Name { get; set; }

    public string prop1 { get; set; }
    public string prop2 { get; set; }
}
public class Info{
    public int Id { get; set; }
    public string Name { get; set; }
}

List<CompleteInfos> Table = new List<CompleteInfos>(); 
// List contains your namse and ids
List<Info> infos = new List<Info>(){
        new Info(){Id = 123 , Name = "someone"},
        new Info(){Id = 223 , Name = "another"},
        new Info(){Id = 456 , Name = "noone"}
} 
foreach(var info in infos)
{
    List<CompleteInfos> selectedInfo = Table.Where(x => x.Id == info.Id || x.Name == info.Name).ToList();

    //selectedInfo  is the list in which you can find all item that have your desired id and name
}

推荐阅读