【问题标题】:How to find all rows of items that have a part in common using LINQ?如何使用 LINQ 查找具有共同部分的所有项目行?
【发布时间】:2013-04-17 04:07:42
【问题描述】:

我需要返回包含部分 (X) 的所有记录(项目),以便之后可以在组或 .GroupBy 中使用它

使用此汇总数据:

ItemName PartName
1        A
1        B
2        A
3        C

所以 Item1 有两部分 (A,B) 等等...

我需要一个 LINQ 查询 - 找到所有具有 A 部分的项目(即项目 1 和 2) - 返回所有这些项目的所有行

1        A
1        B
2        A

请注意,最终结果返回了行 (1 B),因为 Item1 具有 PartA,因此我需要取回 Item1 的所有行。

我在看类似的东西:

let items = from data in summary where data.PartName == A select new { data.ItemName }  // to get all the items I need

但是,既然我有了那个列表,我需要使用它来获取列出的所有项目的所有行,但我似乎无法弄清楚......

实际源码(供参考): 笔记: 配方 = 项目 成分=部分 (我只是想让它更简单)

            ViewFullRecipeGrouping = (
                from data in ViewRecipeSummary
                group data by data.RecipeName into recipeGroup
                let fullIngredientGroups = recipeGroup.GroupBy(x => x.IngredientName)
                select new ViewFullRecipe()
                {
                    RecipeName = recipeGroup.Key,
                    RecipeIngredients = (
                        from ingredientGroup in fullIngredientGroups
                        select new GroupIngredient()
                        {
                            IngredientName = ingredientGroup.Key
                        }
                    ).ToList(),
                    ViewGroupRecipes = (
                        from data in ViewRecipeSummary

                        // this is where I am looking to add the new logic to define something I can then use within the next select statement that has the right data based on the information I got earlier in this query.
                        let a = ViewRecipeSummary.GroupBy(x => x.RecipeName)
                            .Where(g => g.Any(x => x.IngredientName == recipeGroup.Key))
                            .Select(g => new ViewRecipe()
                                {
                                    RecipeName = g.Key,
                                    IngredientName = g.Select(x => x.IngredientName)
                                })                                                                  

                        select new GroupRecipe()
                        {
            // use the new stuff here

                        }).ToList(),
                }).ToList();

任何帮助将不胜感激。 谢谢,

【问题讨论】:

    标签: linq


    【解决方案1】:

    我相信这可以满足您的需求:

    var data = /* enumerable containing rows in your table */;
    var part = "X";
    var items = new HashSet<int>(data
        .Where(x => x.PartName == part)
        .Select(x => x.ItemName));
    var query = data.Where(x => items.Contains(x.ItemName));
    

    如果我最后理解了您的评论,我相信这也可以满足您的要求:

    var query = data
        .GroupBy(x => x.ItemName)
        .Where(g => g.Any(x => x.PartName == part))
        .Select(g => new
        {
            ItemName = g.Key,
            PartNames = g.Select(x => x.PartName)
        });
    

    【讨论】:

    • 你能用 LET 或 INTO 语句做第二个吗?这个想法是我需要将它嵌入到现有的 LINQ 语句中,这样我就不能单独创建一个新的 var。
    • @JSchwartz 我不明白评论。您能否在您的问题中添加更多详细信息,并举例说明“将其嵌入现有的 LINQ 语句”的含义?
    • 在您的示例中,PartNames 是什么?
    • 我在原帖中添加了完整的源代码。我从你那里得到的代码添加为“let a ....”,因为这是我需要施展魔法的地方,以便在下一个 select new GroupRecipe() 部分中我有适当的数据可以使用
    • 看起来您能够将查询集成到更大的查询中。现在有什么问题?如果你能用英语描述你想要完成的事情,那将是一个很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2021-08-09
    • 2015-04-28
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多