EntityFramework - contains query of composite key

前端 未结 7 1573
渐次进展
渐次进展 2020-11-22 06:06

given a list of ids, I can query all relevant rows by:

context.Table.Where(q => listOfIds.Contains(q.Id));

But how do you achieve the sa

7条回答
  •  春和景丽
    2020-11-22 07:09

    You can create a collection of strings with both keys like this (I am assuming that your keys are int type):

    var id1id2Strings = listOfIds.Select(p => p.Id1+ "-" + p.Id2);
    

    Then you can just use "Contains" on your db:

    using (dbEntities context = new dbEntities())
                {
                    var rec = await context.Table1.Where(entity => id1id2Strings .Contains(entity.Id1+ "-" + entity.Id2));
                    return rec.ToList();
                }
    

提交回复
热议问题