0

Hi I'm trying to concat an linq expression Ex: I have an List<string[]> where I need to read this on a loop i need to creat a query like this

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

the follow code is what I'm working on

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

But it's creating a query like this

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

Is there someway to concat this ?

2 Answers2

0

You can use the approach below. İt is simple and usable. Tou dont need to deal with dynamic expressions.

var result = list.Where(x => arr.Any(t => <your_condition>))
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

I suppose it can help you

If we consider CompleteInfos as your table then :

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
}
M.Armoun
  • 1,075
  • 3
  • 12
  • 38
  • Well one of the reasons that I'm not using toList is because I don't want to reach the database for each line, I may have more than thousand itens so I'd like to make only one query, and I'm afraid that this may heavy the connection – Marcos Brinner pikatoons Dec 28 '18 at 16:41
  • @MarcosBrinnerpikatoons this procedure should not request the data base each time. for example you run a `SELECT` query on the database and then all data will be inside the RAM – M.Armoun Dec 28 '18 at 16:46