EntityFramework - contains query of composite key

前端 未结 7 1504
渐次进展
渐次进展 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<CK> 
    {
        new CK { id1 = 1, id2 = 2 },
        new CK { id1 = 1, id2 = 3 },
        new CK { id1 = 2, id2 = 4 }
    };
    
    IQuerable<CK> 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();
    
    0 讨论(0)
提交回复
热议问题