展平集合的集合以便在 WPF 数据网格上显示

发布于 2024-10-02 17:56:09 字数 233 浏览 1 评论 0原文

我正在 Telerik WPF 数据网格上使用堆叠标头。我解决这个问题的方法是创建数据的扁平化视图。例如。

它将显示如下:

Person A |乙 | 人C 人
月 |收入月份|收入月份|收入

我有一组人员,其中包含他们各自的月份和收入列表。

因此,我想让每个人处于同一水平,或者用更简单的术语来说,我想创建一个列表,其中水平显示所有月份和收入。 解决这个问题的最佳方法是什么?

I am working with stacked headers on a Telerik WPF data grid. The way I was approaching the problem was to create a flattened view of my data. For example.

it would display like the following:

Person A | Person B | Person C
Month | Earning Month|Earning Month|Earning

I have a collection of persons containing their respective lists of months and earning.

Therefore I want to bring each person on the same level or in easier terms, I want to create a single list with all the months and earnings available horizontally.
What would the best way to approach this?

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

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

发布评论

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

评论(2

爱的故事 2024-10-09 17:56:09

我不确定您是否想要一个包含多次的人员的列表,或者只是包含收入的列表。我根据不同的要求和我对您的类结构所做的猜测,为您提供了几个解决方案。

首先假设您想要一个列表,其中每个项目都有一个人、月份和收入,并且您有一个人员类的结构,其中包含两个列表(一个用于收入,一个用于在索引上同步的月份),您可以使用以下用于创建枚举的 LINQ 查询的变体

var flatList = from person in persons
                from month in person.Month.Select((month, index) => new {Month = month, Index = index})
                from amount in person.Amount.Select((amount, index) => new { Amount = amount, Index = index })
                where month.Index == amount.Index
                select new { Name = person.Name, Month = month.Month, Amount = amount.Amount};

如果您只需要给出相同结构的月份和金额,您可以编写以下内容:

var flatList = from person in persons
                from month in person.Month.Select((month, index) => new {Month = month, Index = index})
                from amount in person.Amount.Select((amount, index) => new { Amount = amount, Index = index })
                where month.Index == amount.Index
                select new { Month = month.Month, Amount = amount.Amount};

如果您有一个人员列表,其中的收入对象与月份和金额相匹配你可以写这样的东西:

var flatList = from person in persons
                       from earning in person.Earnings
                       select new { Pers = person.Name, Month = earning.Month, Amount = earning.Amount };

或者,如果你不想要你要使用的人:

var flatList = from person in persons
                           from earning in person.Earnings
                           select new {Month = earning.Month, Amount = earning.Amount };

我希望其中之一就是你正在寻找的人,否则请进一步澄清你的问题,因为我误解了。

I am unsure if you want a list that has the people in it multiple times or just the list with the earnings. I give you a couple solutions based upon the differing requirements and the guesses that I had to make about your class structure.

First assuming you want a list of where each item has a person, the month, and the earnings and you have the structure of a person class with two contained lists (one for earnings and one for months that are synchronized on indexes you can use a variant of the following LINQ query to create the enumeration

var flatList = from person in persons
                from month in person.Month.Select((month, index) => new {Month = month, Index = index})
                from amount in person.Amount.Select((amount, index) => new { Amount = amount, Index = index })
                where month.Index == amount.Index
                select new { Name = person.Name, Month = month.Month, Amount = amount.Amount};

If instead you wanted just the month and amounts given the same structure you'd write the following:

var flatList = from person in persons
                from month in person.Month.Select((month, index) => new {Month = month, Index = index})
                from amount in person.Amount.Select((amount, index) => new { Amount = amount, Index = index })
                where month.Index == amount.Index
                select new { Month = month.Month, Amount = amount.Amount};

If instead you had a list of persons that has an earnings object which matches the month and the amount you could write something like this:

var flatList = from person in persons
                       from earning in person.Earnings
                       select new { Pers = person.Name, Month = earning.Month, Amount = earning.Amount };

Or again if you didn't want the person you'd use:

var flatList = from person in persons
                           from earning in person.Earnings
                           select new {Month = earning.Month, Amount = earning.Amount };

I hope one of those is what you were looking for, otherwise please clarify your question further because I misunderstood.

玩心态 2024-10-09 17:56:09

正如其他人所说,如果不查看更多代码,就很难具体说明。但是,一般来说,扁平化集合是的事情SelectMany() LINQ 扩展方法 就是为此而设计的。

As others have said, it's difficult to be specific without seeing more of your code. But, in general, flattening collections is the sort of thing that the SelectMany() LINQ extension method is made for.

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