开发者

Flatten out a Collection of Collections in order to display on a WPF data grid

开发者 https://www.devze.com 2023-01-24 11:19 出处:网络
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.

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 th开发者_JAVA百科e 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消