加入3表和组主题越来越重复的结果

问题描述 投票:0回答:1

我想加盟3台tbRecipe - tbImageRecipe - tbMaterial和组主题为一个大名单。

这是我的代码:

 var result = (from c in db.tbRecipe.Distinct()                                 
                              join s in db.tbImageRecipe.Distinct()
                              on c.ID equals s.RecipeID
                              join material in db.tbMaterial.Distinct() on 
 c.ID equals material.RecipeID 
                              group new {  s.ImageUrl, 
  material.MaterialName,material.MaterialValue,material.RecipeID }  by new { 
  c.ID, c.Name, c.CaloryValue, c.CoockTime ,c.ViewCount, c.VideoURL} 
                             into g
                             orderby g.Key.ViewCount descending
                              select g).Take(10).ToList();

                var lstItems = new List<ImageModel>();
                var MaterialItems = new List<MatrialModel>();


                foreach (var r in result)
                {
                    var item = new ImageModel { ImageRecipes = new 
          List<tbImageRecipe>() , ItemsofMaterial = new List<tbMaterial>() }  
  ;


                    foreach (var s in r)
                    {
                        item.ImageRecipes.Add(new tbImageRecipe
                        {
                            ImageUrl = s.ImageUrl
                        });
                        item.ItemsofMaterial.Add(new tbMaterial
                        {
                            MaterialName = s.MaterialName,
                            MaterialValue = s.MaterialValue,
                            RecipeID = s.RecipeID
                        });
                    }
                    lstItems.Add(item);


                }

                return Json(lstItems.Distinct());

我越来越多的结果是这样的:下面的结果在数据库3图像URL和一个重要项目:

  "ImageRecipes": [
        {
            "ID": 0,
            "ImageUrl": "https://findicons.com/files/icons/1187/pickin_time/128/lettuce.png",
            "RecipeID": null
        },
        {
            "ID": 0,
            "ImageUrl": "https://findicons.com/files/icons/1187/pickin_time/128/lettuce.png",
            "RecipeID": null
        },
        {
            "ID": 0,
            "ImageUrl": "https://findicons.com/files/icons/1187/pickin_time/128/lettuce.png",
            "RecipeID": null
        }
    ],
    "ItemsofMaterial": [
        {
            "ID": 0,
            "MaterialName": "نمک",
            "MaterialValue": "200 گرم",
            "RecipeID": 9
        },
        {
            "ID": 0,
            "MaterialName": "نمک",
            "MaterialValue": "200 گرم",
            "RecipeID": 9
        },
        {
            "ID": 0,
            "MaterialName": "نمک",
            "MaterialValue": "200 گرم",
            "RecipeID": 9
        }
    ]

我做错了吗?

sql-server linq
1个回答
2
投票

所以,你有Recipes的序列,每个Recipe由唯一的主键标识。

你也有ImageRecipes表,通过唯一的主键标识。有RecipesImageRecipes之间的一个一对多的关系:每Recipe具有零个或多个ImageRecipes,每ImageRecipe属于使用外键只有一个Recipe

同样你有一个一对多MaterialsRecipe之间的Materials,每Material属于一个Recipe

每当你想,像一个“学校对他的学生”的“与(部分或全部),他的许多分项项”,“客户与他的订单”或“订单以其OrderLines”,你应该考虑到使用Enumerable.GroupJoin。在参数指定的主键和外键,并选择您在最终结果所需的属性。

顺便说一句:你为​​什么要使用Distinct()?表中的所有元素都有一个唯一的主键,没有他们,所以他们已经是唯一的。

var lstItems = recipes.GroupJoin(imageRecipes, // GroupJoin recipes and imagerecipes
    recipe => recipe.Id,                       // from every recipe take the primary key
    imageRecipe => imageRecipe.recipeId        // from every imageRecipe take the foreign key
    (recipe, imageRecipes) => new              // from every recipy with all its imageRecipes
    {                                          // make one new:
        Recipe = recipe,
        ImageRecipes = imageRecipes,
    })

    // Groupjoin the Materials:
    .GroupJoin(tblMaterials,
    joinResult => joinResult.Recipe.Id    // from the Recipe take the primary key
    material => material.RecipeId         // the the Material take the foreign key
    (joinResult, materials) => new        // from every Recipe-with-its-imageRecipes
    {                                     // and the matching materials, make one new:
        // select the image properties you plan to use
        Id = joinResult.Image.Id,
        Name = joinResult.Image.Name,
        ...

        // all ImageMaterials of this Image
        ImageMaterials = joinResult.ImageMaterials
            .Where(imageMaterial => ...)       // if you want only some ImageMaterials of this Image
            .Select(imageMaterial => new
            {
                // select the imageMaterials you plan to use
                Id = imageMaterial.Id,
                ...
            })
            .ToList(),

            Materials = materials.Select(material => new
            {
                ... // the material properties you plan to use.
            })
            .ToList(),    
    }              

结果:食谱,各有其ImageRecipes和材料。

在您的内连接你做一个奇怪的GROUPBY,我不明白。唉,你忘了写了确切的规格,我不能从查询中提取规范,因为这查询不给你想要的结果。所以你必须做GROUPBY yourseld

© www.soinside.com 2019 - 2024. All rights reserved.