LINQ concat谓词OR

时间:2018-12-28 16:27:24

标签: c# linq lambda predicate

嗨,我正在尝试连接一个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')

有什么办法吸引这个吗?

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法。它很简单并且可用。您不需要处理动态表达式。

var result = list.Where(x => arr.Any(t => <your_condition>))

答案 1 :(得分:0)

我想它可以帮助您

如果我们将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
}