使用 LINQ 标准化数据
假设我们有一些非规范化数据,如下所示:
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);
我需要对其进行规范化,例如获得 IEnumerable<儿童>填充了 Child.Parent 和 Child.Parent.GrandParent。
命令式的方式或多或少是明确的。使用 Linq 会更短吗?
在一个查询中效果更好,并且这应该可以扩展到更多实体。
我尝试过单独创建 IEnumerable< GrandParent >,然后 IEnumerable<父>与分配等。
请提示这可以通过功能方式实现吗?
Assume we have some denormalized data, like this:
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);
I need to normalize it, e.g. to get IEnumerable< Child > with Child.Parent and Child.Parent.GrandParent filled.
Imperative way is more or less clear. Will it be shorter with Linq?
Better in one query, and this should be expandable for more entities.
I tried something like separately create IEnumerable< GrandParent >, then IEnumerable< Parent > with assigning etc.
PLease make a hint could this be achieved in a functional way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 group by 完全执行您想要的操作。不幸的是,我对 C# LINQ 语法的了解有限,所以我只能向您展示调用扩展方法 GroupBy 的方法。
You can do exactly what you want using group by. Unfortunately my knowledge of the C# LINQ syntax is limited, so I just can show you the way calling extension method GroupBy.
Linq 确实做了相反的事情。 IE。如果你将其标准化,你可以很容易地说
“做你所要求的事情更棘手”。像这样的东西
我不相信这比命令式版本读起来更好。
Linq really does the opposite of this. ie. If you had it normalised, you could easily say
To do what you're asking is more tricky. Something like this
I'm not convinced this reads better than the imperative version would.
最基本的方法是使用匿名变量:
如果您想使用具体类来执行此操作,我建议创建一个
Person
类,其构造函数采用IEnumerable< /code> 代表正在构造的
Person
的子级。然后您可以这样做:这些解决方案对您有用吗?
如果您想要不同的
GrandParent
、Parent
和Child
类型,那么您应该能够修改最后一个示例以适应。The most basic way of doing this would be with anonymous variables:
If you wanted to do this with concrete classes I would suggest creating a
Person
class with a constructor that takes anIEnumerable<Person>
representing the children of thePerson
being constructed. Then you could do this:Do either of these solutions work for you?
If you want different
GrandParent
,Parent
&Child
types then you should be able to modify the last example to suit.