按标识符分组时的 LINQ to custom class
LINQ to custom class when grouped by identifier
我有一个数据 table,我需要将其中的数据解析为一组易于管理的 class 数据。在大多数情况下,这不是一项艰巨的任务,但是,我需要将这些自定义 classes 中的项目组合在一起,以便理解它们。
类
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>();
}
Table
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,将它们链接回子项并在那里创建 class。我还没有编写这部分代码,但如果这是唯一的方法,那很好。我只是希望 LINQ 中有一些东西可以帮助我实现我的目标。
您需要一个简单的 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。
编辑:
在上面的查询中,如果 ChildBooks
是类型 String
那么你会得到逗号分隔的书,但是如果你想要 List<String>
作为 ChildBooks(那么显然它们不会被逗号分隔)你可以这样做:-
ChildBooks = x.Select(b => b.Field<string>("Books")).ToList();
找出我需要的最终答案。 @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()
});
我有一个数据 table,我需要将其中的数据解析为一组易于管理的 class 数据。在大多数情况下,这不是一项艰巨的任务,但是,我需要将这些自定义 classes 中的项目组合在一起,以便理解它们。
类
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>();
}
Table
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,将它们链接回子项并在那里创建 class。我还没有编写这部分代码,但如果这是唯一的方法,那很好。我只是希望 LINQ 中有一些东西可以帮助我实现我的目标。
您需要一个简单的 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。
编辑:
在上面的查询中,如果 ChildBooks
是类型 String
那么你会得到逗号分隔的书,但是如果你想要 List<String>
作为 ChildBooks(那么显然它们不会被逗号分隔)你可以这样做:-
ChildBooks = x.Select(b => b.Field<string>("Books")).ToList();
找出我需要的最终答案。 @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()
});