【问题标题】:Join multi tables and group by Id in LINQ在 LINQ 中加入多表并按 Id 分组
【发布时间】:2015-03-03 17:17:37
【问题描述】:

我想按 categoryId 显示列表产品的名称组,这是我的代码:

我想查看我的显示结果:

Desktop
|_ PC HP - Red
|_ PC Dell - Yellow
|_ PC Asus - Red
SmartPhone
|_ Lumia 720 - Blue

我的 GroupModel:

public class GroupModel
{
    public Category Categories { get; set; }
    public Product Products { get; set; }
}

我的控制器:

List<Category> listCategories = new List<Category>
{
    new Category {Id = 1, CateName = "SmartPhone"},
    new Category {Id = 2, CateName = "Laptop"},
    new Category {Id = 3, CateName = "Desktop"},
};

List<Product> listProducts = new List<Product>
{
    new Product {Id = 1, ProdName = "Lumia 720", CategoryId = 1, ColorId = 2},
    new Product {Id = 2, ProdName = "PC HP", CategoryId = 3, ColorId = 1},
    new Product {Id = 3, ProdName = "PC Dell", CategoryId = 3, ColorId = 1},
    new Product {Id = 4, ProdName = "Laptop Lenovo", CategoryId = 2, ColorId = 2},
    new Product {Id = 5, ProdName = "Lumia 920", CategoryId = 1, ColorId = 2},
    new Product {Id = 6, ProdName = "Laptop Dell", CategoryId = 2, ColorId = 3},
    new Product {Id = 7, ProdName = "Laptop HP", CategoryId = 2, ColorId = 3}
};

List<Color> listColor = new List<Color>
{
    new Color {ColorId = 1, ColorName = "Blue"},
    new Color {ColorId = 2, ColorName = "Yellow"},
    new Color {ColorId = 3, ColorName = "Red"}
};

var query = from c in listCategories
join p in listProducts on c.Id equals p.CategoryId
select new GroupModel
{
    Categories = c,
    Products = p
};

return View(query.ToList());

这是我绑定列表的观点。我正在使用 GroupModel 来嵌套 ProductModel 和 CategoryModel

@model  IEnumerable<Test.Models.GroupModel>
@foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => item.Categories.CateName)
            </td>
        </tr>
        <tr>
            <td>
                @Html.Label("|__ ")  @Html.DisplayFor(model => item.Products.ProdName)
            </td>
        </tr>
    }

【问题讨论】:

标签: c# linq entity-framework group-by


【解决方案1】:

您需要ProductsCategories 的左外连接并在CategoryID 上对产品进行分组:

var query = from p in listProducts
                join c in listCategories on p.CategoryId equals c.Id  into e
                from j in e.DefaultIfEmpty()
                group p by p.CategoryId into g
                select new { Products = g, CategoryId = g.Key };

更新:

var query = from p in listProducts
                join c in listCategories on p.CategoryId equals c.Id  into e
                from j in e.DefaultIfEmpty()
                group p by new { j.Id,j.CateName} into g
                select new GroupModel
                           { 
                               Products = g.ToList(), 
                               CategoryId = g.Key.Id,
                               CateogryName=g.Key.CateName 
                           };

型号:

public class GroupModel
{
    public int CategoryId { get; set; }
    public string CateogryName { get; set; }
    public List<Product> Products { get; set; }
}

Working EXAMPLE FIDDLE

更新 2:

var query =
            from p in listProducts
            join cl in listColor on p.ColorId equals cl.ColorId
            join c in listCategories on p.CategoryId equals c.Id into e
            from j in e.DefaultIfEmpty()group p by new
            {
            j.Id,cl.ColorId,j.CateName,cl.ColorName
            }

                into g
                select new GroupModel
                {
                Products = g.ToList(), CategoryId = g.Key.Id, CateogryName = g.Key.CateName,ColorId = g.Key.ColorId,ColorName = g.Key.ColorName
                }

        ;
        foreach (var item in query)
        {
            Console.WriteLine("CategoryName: {0}", item.CateogryName);
            //Console.WriteLine("ColorName: {0}", );
            foreach(var product in item.Products)
            {
                Console.WriteLine("Product: |_ {0} - {1}", product.ProdName,item.ColorName);
            }
        }

输出:

类别名称:智能手机
产品:|_ Lumia 720 - 黄色
产品:|_ Lumia 920 - 黄色
类别名称:桌面
产品:|_ PC HP - 蓝色
产品:|_ PC 戴尔 - 蓝色
类别名称:笔记本电脑
产品:|_ 联想笔记本电脑 - 黄色
类别名称:笔记本电脑
产品:|_ 戴尔笔记本电脑 - 红色
产品:|_ HP 笔记本电脑 - 红色

【讨论】:

  • 如果您仔细查看 OP 视图,他使用的是单个 foreach 循环而不是嵌套循环,这正是我发布的答案,但 OP 再次编辑了他的问题。
  • @RahulSingh 其中 OP 说 hw 想要避免嵌套循环,在这种情况下正确的方法是分组,而不是一次又一次地复制类别对象
  • 我知道这是正确的方法,但我只是指出 OP 在视图中发布的内容。我知道这是设计的问题:)
  • 是的,我知道 OP 需要更改模型或需要显示重复项。
  • 感谢@EhsanSajjad 的支持。你的代码工作正常:)
猜你喜欢
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2015-11-18
  • 1970-01-01
相关资源
最近更新 更多