【问题标题】:LINQ join 2 listsLINQ 加入 2 个列表
【发布时间】:2014-10-01 15:07:25
【问题描述】:

我想根据标签选择我的产品。我将标签列表传递给该方法。每个产品可以有一个或多个标签,每个标签可以应用于一个或多个产品。

我无法在 LINQ 中翻译,但在 SQL 中,查询将是:

SELECT * 
FROM products, tagproducts, tags
where products.productId = tagproducts.Product_productId
and tagproducts.Tag_tagId = tags.tagId
and tags.tagId IN (1,2);

我想我必须使用 join,但找不到获得我想要的方法.. 有什么想法吗?

【问题讨论】:

  • 如果模型中没有tagproducts(作为您对已删除答案之一的评论),您将如何从中获取数据?将其添加到模式中,然后使用@DavidG 回答。
  • 哦可以吗?我想因为它是自动生成的,所以我不能将它添加到模型中,但我可以试一试^^

标签: sql .net linq


【解决方案1】:
var tagIDs = new List<int> { 1, 2 };
var products = Products.Where(x => x.TagProducts.Any(y => tagIDs.Contains(y.TagID )));
//to get info from the products, . into them
var tagNames = products.TagProducts.Select(x => x.Tags.TagName); //or whatever.

【讨论】:

    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多