Linq Aggregate 使用嵌套类生成字符串

发布于 2024-10-21 07:32:17 字数 917 浏览 1 评论 0 原文

var outline = Grandparents.Select(
    x =>
    x.Parents.Select(
        y =>
        y.Children.Aggregate(string.Empty, (current, child) => string.Format("{0}{1},{2},{3},{4}\n",
            current, x.Grandparent,
            y.Parent,
            child.Name,
            child.Age))));

Grandparents 是一个有两个成员的类:

string Grandparent
List<Parent> Parents

Parents 是一个有两个成员的类:

string Parent
List<Child> Children

Child 是一个有两个成员的类:

string Name
int Age

我想使用 Linq 生成一个字符串,我将写入文本文件,例如:

Grandpa Walter, Parent William, Child Chris, Age 11
Grandpa Walter, Parent Sue, Child Alice, Age 7
Grandpa Walter, Parent Sue, Child Sam, Age 7
Grandpa Eugene, Parent David, Child Joe, Age 17

上面的代码生成String 的 IEnumerable 的 IEnumearable。我只想产生一个“字符串”

var outline = Grandparents.Select(
    x =>
    x.Parents.Select(
        y =>
        y.Children.Aggregate(string.Empty, (current, child) => string.Format("{0}{1},{2},{3},{4}\n",
            current, x.Grandparent,
            y.Parent,
            child.Name,
            child.Age))));

Grandparents is a class with two members:

string Grandparent
List<Parent> Parents

Parents is a class with two members:

string Parent
List<Child> Children

Child is a class with two members:

string Name
int Age

I want to use Linq to produce a string that I'll write to at text file, for example:

Grandpa Walter, Parent William, Child Chris, Age 11
Grandpa Walter, Parent Sue, Child Alice, Age 7
Grandpa Walter, Parent Sue, Child Sam, Age 7
Grandpa Eugene, Parent David, Child Joe, Age 17

The above code produces an IEnumearable of IEnumerable of String. I want to produce just a "string"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2024-10-28 07:32:17

只需使用 SelectMany() 展平序列并使用 string.Join() 进行聚合:

string result =  string.Join(Environment.NewLine, outline.SelectMany( x=>x));

just flatten the sequence using SelectMany() and use string.Join() to aggregate:

string result =  string.Join(Environment.NewLine, outline.SelectMany( x=>x));
听你说爱我 2024-10-28 07:32:17

试试这个:

string result = (from grandParent in grandParents
                 from parent in grandParent.Parents
                 from child in parent.Children
                 select string.Format("{0}, {1}, {2}, {3}", grandParent.Name, parent.Name, child.Name, child.Age))
                .Aggregate((a, b) => a + Environment.NewLine + b);

无论如何,我建议您使用 File.WriteAllLines 保存文本文件,而不是使用 Aggregate:

var results = from grandParent in grandParents
              from parent in grandParent.Parents
              from child in parent.Children
              select string.Format("{0}, {1}, {2}, {3}", grandParent.Name, parent.Name, child.Name, child.Age)

File.WriteAllLines("myfile.txt", results);

Try this:

string result = (from grandParent in grandParents
                 from parent in grandParent.Parents
                 from child in parent.Children
                 select string.Format("{0}, {1}, {2}, {3}", grandParent.Name, parent.Name, child.Name, child.Age))
                .Aggregate((a, b) => a + Environment.NewLine + b);

Anyway, instead of using Aggregate, I suggest you to save your text file using File.WriteAllLines:

var results = from grandParent in grandParents
              from parent in grandParent.Parents
              from child in parent.Children
              select string.Format("{0}, {1}, {2}, {3}", grandParent.Name, parent.Name, child.Name, child.Age)

File.WriteAllLines("myfile.txt", results);
戏剧牡丹亭 2024-10-28 07:32:17

上面的查询将返回一个 IEnumerable>,其中每个元素代表祖父母,每个元素中的每个字符串代表每个父母的聚合。

虽然您可以将其转换为第二个聚合,但这似乎是为了使用 LINQ 而使用 LINQ。使用 StringBuilder 并迭代您的集合会更好。您将生成仅比 LINQ 查询稍长的代码,并且可以更轻松地了解实际发生的情况。

由于您的查询没有定义它,我不确定您希望如何合并每个父级的结果。如果您能澄清这一点,我将更新此答案以反映这一点。

The above query is going to return an IEnumerable<IEnumerable<string>>, where each element represents a grandparent, and each of the strings within each element represent your aggregate for each parent.

While you could transform this into a second aggregate, this seems like using LINQ for the sake of using LINQ. You would be much better off using a StringBuilder and just iterating over your collection. You'll produce code that is only slightly longer than your LINQ query, and it will be much easier to see what's actually going on.

Since your query doesn't define it, I'm not certain how you want to combine the results from each parent. If you can clarify that, I'll update this answer to reflect that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文