-1

I'm kinda new to Lambda many things work already but right now I'm getting to the limit of my understanding.

I have a WPF App, where I want to show some stats that I recorded into a Json file.

The file looks like this:

Date, Name, Position, Region

Now I want to add all those Information into a ListView

The ListView looks more or less like this:

<ListView x:Name="lbtNames" FontSize="16" HorizontalContentAlignment="Left" Margin="24,122,207,19">

<ListView.View>

<GridView>

<GridViewColumn Header="Hero:" Width="120" DisplayMemberBinding="{Binding Name}"></GridViewColumn>

<GridViewColumn Header="Count: " Width="50" DisplayMemberBinding="{Binding Count}"></GridViewColumn>

</GridView>

</ListView.View>

</ListView>

And the Code behind:

var query = _recordList

.GroupBy(x => x.Name, (y, z) => new { Name= y, Count = z.Count() })

.OrderByDescending(o => o.Count);

lbtNames.ItemsSource = query;

So far so easy now I also would like to add some more Information like Average Position and maybe a small graph that shows over time the positions, well this will be made seperatly but to my ListView I would add the Column Average Position somehow, is it possible to add more to my Lambda statement?

boonwin
  • 59
  • 5

1 Answers1

3

Yes, you just make it a multi line statement inside a code block, with a return

Your this:

.GroupBy(x => x.Name, (y, z) => new { Name= y, Count = z.Count() })

Is like this:

.GroupBy(x => x.Name, (y, z) => {
  return new { Name= y, Count = z.Count() };
})

So you can:

.GroupBy(x => x.Name, (y, z) => {
  int i = SomeCalc();
  var r = new { Name= y, Count = z.Count(), Result = i };
  return r;
})
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • but isnt r.Name read only? and can I also add another column like AvgPosition where I just do something like Position/Count where Name = Name – boonwin Dec 26 '20 at 13:16
  • You can add AvgPosition certainly, and you could calculate it yourself, but I'm not sure why you need to calc it yourself when LINQ has an `Average()`.. – Caius Jard Dec 26 '20 at 15:21