通过 LINQ 对多列进行分组

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

我有一个来自 SP 的数据表,其中包含类别、子类别和消息。对于一个类别有多个子类别,对于一个子类别有多个消息文本。因此,在获取服务中,我需要在对象中以数组格式发送这些类别。

类别名称 子类别名称 留言文字
取景 遮住脸 看不清孩子的脸
取景 遮住脸 检测到食物
灯光 照明不当 检测到光线不足
灯光 照明不当 不推荐
灯光 太暗 检测到孩子背后有太阳

我正在使用 asp.net core 8 应用程序。我有两个模型课

因此,在现有服务中,我需要添加这些响应详细信息,因此在 DAL 层中创建了一个私有方法。

我正在尝试为类别名称和子类别名称添加 groupby,但出现一些错误**“无法将类型 'System.Collections.Generic.IEnumerable>' 隐式转换为 'String'”** .

我写的代码;

private List<ARMCategory> CategoryDetailsCUP(DataSet mediaReviewDetails)
{
    List<ARMCategory> ARMCategory = new List<ARMCategory>();
    if (mediaReviewDetails != null)
    {
        foreach (DataRow dataRow in mediaReviewDetails.Tables[2].Rows)
        {
            List<ARMSubCategory> results2 = ARMCategory
            .GroupBy(p => new { p.CategoryName, p.SubCategoryNames },
                     (k, c) => new ARMCategory()
                     {
                         CategoryName = k.CategoryName,
                         SubCategoryNames = c.Select(cs => cs.SubCategoryNames.ToList()),
                         MessageTexts = c.Select(cs => cs.MessageTexts.ToList())
                     }
                    ).ToList();
            return ARMCategory;
        }
    }
    //return result2;
}

例如我需要的输出, 对于类别框架,我们有一个 Facecovered 子类别,因此它需要映射到类别框架下,对于 Facecovered 子类别,我们有多个 MessageText,因此这些文本应该映射到各自的子类别下。像这样,每个类别都需要映射各自的子类别和消息文本。 所以需要 C# 代码

c# asp.net-core-8
1个回答
0
投票

好的。我尝试创建一个示例来指出错误。

  1. 您没有使用任何数据,正在运行。
  2. 因为您没有提供类结构,所以我会假设新的 ARMCategory 类应该是什么结构。
  3. 你有ARMCategories.GroupBy(其中SubCategoryName是一个字符串)并且在 同时你有 (k, c) => new ARMCategory() - 其中 SubCategoryName 是一个列表

因此您有多种选择:

  1. 可以将列表聚合到一个字符串中
  2. 创建一个新类,其中 subCategory 是一个列表
void Main()
{
    List<ARMCategory> ARMCategories = new List<ARMCategory>(){
        new ARMCategory() { CategoryName = "Framing", SubCategoryName = "Face Covered", MessageText = "Child's face can not be clearly seen" },
        new ARMCategory() { CategoryName = "Framing", SubCategoryName = "Face Covered", MessageText = "food detected" },
        new ARMCategory() { CategoryName = "Lighting",SubCategoryName = "Incorrect lighting", MessageText = "Poor lighting detected" },
        new ARMCategory() { CategoryName = "Lighting",SubCategoryName = "Incorrect lighting", MessageText = "Not recommended" },
        new ARMCategory() { CategoryName = "Lighting",SubCategoryName = "Too dark", MessageText = "Sun behind the Child detected"}
    };
    var results2 = ARMCategories
                    .GroupBy(p => new { p.CategoryName, p.SubCategoryName },
                             (k, c) => new NEW_Class_Structure
                             {
                                 CategoryName = k.CategoryName,
                                 SubCategoryNames = c.Select(s => s.SubCategoryName).ToList(),//.Aggregate((a, b) => $"{a}, {b}"),
                                 MessageTexts = c.Select(cs => cs.MessageText).ToList()
                             }
                            ).ToList().Dump();


}

public class ARMCategory{
    public string CategoryName{get;set;}
    public string SubCategoryName {get;set;}
    public string MessageText {get;set;}
}

public class NEW_Class_Structure
{
    public string CategoryName { get; set; }
    public List<string> SubCategoryNames { get; set; }
    public List<string> MessageTexts { get; set; }
}


enter image description here

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