0

我需要返回所有包含部分 (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

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

实际源代码(供参考):注意:Recipe = ITEM Ingredient = PART(我只是想让它更简单)

            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();

任何帮助将非常感激。谢谢,

4

1 回答 1

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)
    });
于 2013-04-17T05:20:55.127 回答