EntityFramework - contains query of composite key

前端 未结 7 1495
渐次进展
渐次进展 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 use Union for each composite primary key:

    var compositeKeys = new List 
    {
        new CK { id1 = 1, id2 = 2 },
        new CK { id1 = 1, id2 = 3 },
        new CK { id1 = 2, id2 = 4 }
    };
    
    IQuerable query = null;
    foreach(var ck in compositeKeys)
    {
        var temp = context.Table.Where(x => x.id1 == ck.id1 && x.id2 == ck.id2);
        query = query == null ? temp : query.Union(temp);
    }
    var result = query.ToList();
    

提交回复
热议问题