【问题标题】:How to group and subgroup and get data from a linq query如何对 linq 查询进行分组和子分组以及获取数据
【发布时间】:2019-04-14 16:20:18
【问题描述】:

嗨。我有三个表,我正在尝试将它们分组和子组。

Table Sections
-------------
Id
SectionName


Table Categories
-------------
Id 
CategoryName
SectionRefId


Table Records
-------------
Id
RecordName
CategoryRefId

我想要实现的是按 SectionName 对所有类别进行分组,并按 CategoryName 对所有记录进行分组,并使用 foreach 循环显示它们。

I tried this using Linkpad the result it not what i exptected </p>


var result = from doc in Categories
             group doc by doc.SectionRefId into docSections
             select new  
             {
                Name = docSections.Key,
                Group = from dl in Records
                        group dl by dl.CategoryRefId into dlRecords
                        select new 
                        {
                            Name = dlRecords.Key,
                            GroupRecords = dlocation
                        }
             };


enter code here

【问题讨论】:

  • 你有相同的sql查询吗?
  • 无 sql 查询,我试图仅使用 linq 来解决。尝试以下解决方案。

标签: c# entity-framework linq model-view-controller


【解决方案1】:

你可以这样做。需要一些加入和分组:

var result = from category in Categories
             join section in Sections on category.Id equals section.ID
             join record in Records on category.Id equals record.CategoryRefId
             group category by {section.SectionName} into g
             group record by {category.categoryName} into p
             select new { CategoryName = g.Key.CategoryName, SectionName = section.SectionName, RecordName = record.RecordName };

【讨论】:

    【解决方案2】:

    如果您遵循entity framework code first conventions,您的类将具有virtual ICollection&lt;...&gt; 属性,可以为您进行分组:

    class Section
    {
        public int Id { get; set; }
        public string SectionName { get; set; }
    
        // every section has zero or more Categories (one-to-many)
        public virtual ICollection<Category> Categories {get; set;}
    }
    
    class Category
    {
        public int Id { get; set; }
        public string CategoryName { get; set; }
    
        // every Category belongs to exactly one Section using foreign key:
        public int SectionId { get; set; }
        public virtual Section Section {get; set;}
    
        // every Category has zero or more Records (one-to-many)
        public virtual ICollection<Record> Records {get; set;}
    }
    
    class Record
    {
        public int Id { get; set; }
        public string RecordName { get; set; }
    
        // every Record belongs to exactly one Category
        public int CategoryId { get; set; }
        public virtual Category Category {get; set;}
    }
    

    在实体框架中,表示数据库表的列 由非虚属性。虚拟属性代表 表之间的关系

    注意,你的表和列可能有不同的标识符,主要是你添加了虚拟属性

    我想要实现的是按 SectionName 对所有 Category 进行分组,并按 CategoryName 对所有 Records 进行分组,并使用 foreach 循环显示它们。

    var results = myDbContext.Sections
        .Where (section => ...)             // only if you don't want all Sections
        .Select(section => new
        {
            // select only the properties you plan to use:
            Id = section.Id,
            Name = section.SectionName,
    
            // This section has zero or more categories:
            Categories = section.Categories
               .Where(category => ...)          // only if you don't want all categories
               .Select(category => new
               {
                   // again, select only the properties you plan to use:
                   Id = category.Id,
                   ...
                   // not needed, you already know the value:
                   // SectionId = category.SectionId,
    
                   // this category has zero or more Records:
                   // you know the drill by now
                   Records = category.Records
                      .Where(record => ...)   
                      .Select(record => new
                      {
                          Id = record.Id,
                          ...
                      })
                      .ToList(),
               })
               .ToList(),
         });
    

    实体框架知道您的一对多关系,并会为您执行适当的 GroupJoins

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多