rud*_*nev 6 linq normalization
假设我们有一些非规范化数据,如下所示:
List<string[]> dataSource = new List<string[]>();
string [] row1 = {"grandParentTitle1", "parentTitle1", "childTitle1"};
string [] row2 = {"grandParentTitle1", "parentTitle1", "childTitle2"};
string [] row3 = {"grandParentTitle1", "parentTitle2", "childTitle3"};
string [] row4 = {"grandParentTitle1", "parentTitle2", "childTitle4"};
dataSource.Add(row1);
Run Code Online (Sandbox Code Playgroud)
我需要对其进行规范化,例如,使用Child.Parent和Child.Parent.GrandParent填充IEnumerable <Child>.
势在必行的方式或多或少都很明确.Linq会缩短吗?
在一个查询中更好,对于更多实体,这应该是可扩展的.
我试过像分别创建IEnumerable <GrandParent>,然后IEnumerable <Parent>分配等.
是否可以通过功能方式实现这一点?
Linq 确实做了相反的事情。IE。如果你把它标准化,你可以很容易地说
from g in grandParents
from p in g.Parents
from c in p.Children
select new { GrandParentName = g.Name, ParentName = p.Name, ChildName = c.Name };
Run Code Online (Sandbox Code Playgroud)
做你所要求的事情更加棘手。像这样的东西
var grandparents = (from g in dataSource
select new GrandParent {
Title = g[0],
Parents = (from p in dataSource
where p[0] == g[0]
select new Parent {
Title = p[1],
Children = from c in dataSource
where p[1] == c[1]
select new
{
Title = c[2]
}
}).Distinct(new ParentTitleComparer())
}).Distinct(new GrandParentTitleComparer());
Run Code Online (Sandbox Code Playgroud)
我不相信这比命令式版本读起来更好。
| 归档时间: |
|
| 查看次数: |
1304 次 |
| 最近记录: |