如果出现其他过滤项,如何创建后续LINQ .join()?

时间:2013-08-05 22:59:04

标签: asp.net linq

如果我有一个lystId,我想要包含MemberProductLyst对象并按LystId过滤。

有关正确实现后续Lamba代码的建议 if(!string.IsNullOrEmpty(lystId)){}阻止初始查询下面???

products = (from p in dc.Products
join t in dc.Tags on p.TagId equals t.TagId
join pi in dc.ProductImages on p.ProductId equals pi.ProductId
join i in dc.Images on pi.ImageId equals i.ImageId
where p.IsActive == true
where t.TagId == new Guid(brandId)
orderby p.CreatedOn descending
select new ProductItem
{
  productImage = i.FileName,
  productId = p.ProductId,
  description = p.Description,
  name = p.Name,
  adImage = t.AdImage
}).Skip(totalItemCount).Take(pageSize);

if (!string.IsNullOrEmpty(lystId))
{
  //Include MemberProductLyst table to Filter by lystId if LystId is available
  var memberLysts = from mpl in dc.MemberProductLysts
  where mpl.LystId == new Guid(lystId)
  select new { mpl.LystId, mpl.ProductId };

  products = (IQueryable<ProductItem>)products.Join(memberLysts, p => p.productId, mpl => mpl.ProductId, (p, mpl) => new {ProductItem = p, MemberProductLyst = mpl });
}

1 个答案:

答案 0 :(得分:0)

这在很大程度上取决于你Join的意图,但我怀疑这可能会产生你正在寻找的结果:

products = products.Where(
    p => memberLysts.Any(mpl => mpl.ProductId == p.productId));