[英]How to create multiple nested conditions against joined tables in linq to entities?
需要帮助将以下查询转换为Linq to Entities。
select * from tableA a join
tableB b on a.ProductId = b.ProductId and a.UserId = b.UserId
where a.ProductId = 60244
and ((b.Column1 = 33 and b.Column2 >= 3) or (b.Column1 = 24 and b.Column2 >= 2))
我从此表达式开始,但是我不知道如何基于列表为tableB创建复杂条件。 我试图遵循predicatebuilder的模式,但遇到了障碍,因为示例不处理联接表。
public IList<tableA> GetSomeStuff(int productId, List<tableB> filters)
{
var query = from a in tableA
join b in tableB
on new
{
ProductId = a.ProductId,
UserId = a.UserId
}
equals
new
{
ProductId = b.ProductId,
UserId = b.UserId
}
where a.ProductId == 6544
select a;
var tableBPredicate = PredicateBuilder.True<tableB>();
foreach (var filter in filters)
{
/* build up tableB conditions here */
tableBPredicate = tableBPredicate.And(p => p.Column1 == filter.Column1);
tableBPredicate = tableBPredicate.And(p => p.Column2 => filter.Column2);
}
// this won't compile because query is of type tableA and not tableB
return query.AsExpandable().Where(tableBPredicate).ToList();
}
如果首先应用谓词,然后将过滤后的集合连接到另一个表,则它应该工作:
var tableBPredicate = PredicateBuilder.True<tableB>();
foreach (var filter in filters)
{
/* build up tableB conditions here */
tableBPredicate = tableBPredicate.And(p => p.Column1 == filter.Column1);
tableBPredicate = tableBPredicate.And(p => p.Column2 => filter.Column2);
}
var tableBQuery = tableB.AsExpandable().Where(tableBPredicate);
var query = from b in tableBQuery
join a in tableA
on new
{
ProductId = b.ProductId,
UserId = b.UserId
}
equals
new
{
ProductId = a.ProductId,
UserId = a.UserId
}
where a.ProductId == 6544
select a;
return query.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.