通常,当您针对属性列表编写 Linq-to-Entity 查询时,请执行以下操作:
var attributes = new List<string>();
...
var result = dbContext.TableA.Where(e => attributes.Contains(e.FieldA));
但是,如果您有一个具有两个或多个属性的对象需要匹配数据库中的一行,该怎么办?
class ClassA_DTO
{
public string AttributeA;
public string AttributeB;
}
...
var attributes = new List<ClassA_DTO>(); //comes from a JSON web API
...
// e.FieldA needs to match attributes.AttributeA
// AND e.FieldB needs to match attributes.AttributeB
var result = dbContext.TableA.Where(e => ???
var result = dbContext.TableA.Where(e => attributes.Select(a => a.AttributeA).Contains(e.FieldA) && attributes.Select(a => a.AttributeB).Contains(e.FieldB));
可以使用逻辑门。