【问题标题】:LINQ to custom class when grouped by identifier按标识符分组时的 LINQ 到自定义类
【发布时间】:2015-05-26 18:48:40
【问题描述】:

我有一个数据表,我需要在其中将其解析为一组可管理的类数据。在大多数情况下,这并不是一项艰巨的任务,但是,我需要将这些自定义类中的项目组合在一起以使它们有意义。

public Parent
{
    Parent(){ }
    public int ParentID = 0;
    public string Name = string.Empty;
    public List<Children> Child = new List<Children>();
}

public Children
{
    public Children() { }
    public string ChildID = 0;
    public string Name = string.Empty;
    public List<string> ChildBooks = new List<string>();
}

表格

 Parent ID |     Parent      | Child ID |     Child     |     Books 
1          | John            | 4        | Suzy          | Grapes of...
1          | John            | 4        | Suzy          | Huck........
1          | John            | 5        | James         | The adven...
2          | Sally           | 4        | Suzy          | Grapes of...
2          | Sally           | 4        | Suzy          | Huck........
2          | Sally           | 5        | James         | The adven...

结果

List<Parent> First index would be
Name: John
Child: 4 Suzy, 5 James

Second index would be
Name: Sally
Child: 4 Suzy (Grapes of..., Huck.....) 5 James (The adven...)

有没有更简单的方法来实现这一点?我当前的方法涉及获取每个Distinct ParentID,将它们链接回子级并在那里创建类。我还没有编写这部分代码,但如果这是唯一的方法,那很好。我只是希望 LINQ 中有一些东西可以帮助我实现我的目标。

【问题讨论】:

  • 为什么John 的子 ID 为 4 和 5?在 DataTable 中,两个孩子都是 4?其他记录也是如此。
  • @RahulSingh 输入错误。现已更正。

标签: c# linq class datatable datatables


【解决方案1】:

找出我需要的最终答案。 @RahulSingh 引导我走向正确的道路。

var query = dt.AsEnumerable().GroupBy(x => x.Field<int>("Parent ID"))
     .Select(x => new Parent
     {
         ParentId = x.Key,
         Children = x.GroupBy(y => y.Field<int>("ChildID")).Select(z => new Children
         {
             ChildID = z.Key,
             Books = z.Select(b => b.Field<int>("Books")).ToList()
         }).ToList()
     });

【讨论】:

    【解决方案2】:

    您需要一个简单的 GroupBy 在 ParentName 上,试试这个:-

    var query = dt.AsEnumerable().GroupBy(x => x.Field<string>("ParentName"))
                                 .Select(x => new Parent
                                        {
                                            Name = x.Key,
                                            Child = x.Select(z => new Children
                                            {
                                                ChildID = z.Field<string>("ChildId"),
                                                Name = z.Field<string>("ChildName"),
                                                //If ChildBooks is `String`
                                                ChildBooks = String.Join(",",
                                                   x.Select(b => b.Field<string>("Books")))
                                            }).ToList()
                                        });
    

    这是示例Fiddle,由于某些错误,它在 fiddler 中不起作用,但您可以复制粘贴并在 Visual Studio 中检查。

    编辑:
    在上面的查询中,如果ChildBooksString 类型,那么你会得到逗号分隔的书籍,但是如果你想要List&lt;String&gt; 作为 ChildBooks(那么显然它们不会被逗号分隔)你可以这样做:-

    ChildBooks = x.Select(b => b.Field<string>("Books")).ToList();
    

    【讨论】:

    • 好的,我想这就是我需要的,但是如果我们将public List&lt;string&gt; ChildBooks = new List&lt;string&gt;(); 添加到子类中呢?你会如何改变以适应这个?查看修改后的问题...
    • 请检查我的更新,但请注意,由于结果中需要逗号分隔的字符串,因此它将是单个字符串,因此您不需要List&lt;string&gt;,而是需要string
    • 收到{"Unable to cast object of type 'System.Int32' to type 'System.String'."}的错误
    • 您的 ChildId 是 int 还是 string?您需要相应地更改传递给Field 的类型。
    • 表中的数据类型为int。改变了我的课程,完美的工作
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多