0

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

关于在初始查询下方的 if (!string.IsNullOrEmpty(lystId)) {} 块内实现后续 Lamba 代码的正确方法的任何建议???

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 });
}
4

1 回答 1

0

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

products = products.Where(
    p => memberLysts.Any(mpl => mpl.ProductId == p.productId));
于 2013-08-05T23:14:09.417 回答