Hi, 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?
Yes, just open curly brackets.
The bracketless syntax is syntactic sugar.
Thanks that worked
You can also create a function with the same signature as the lambda and pass it instead.
Lambda parameters are just Higher Order Functions
and they can be expressed as non-anonymous functions.
Lets take filter
(LINQ: Where) as an exercise. Its signature is:
[a] -> (a -> Bool) -> [a]
The lambda is the second parameter (a -> Bool)
.
You can extract that to an function with signature of a -> Bool
like this
And pass it to filter
once its needed. Since the C# signature of Where uses an extension method for the first parameter it will be called like so:
listOfModels.Where(x => FilterBySomething(x));
I would personally advice against doing any array or value modifications inside of the lambda parameters for LINQ, and instead using the results of said functions.
LINQ provides loads of functional expressions, one of which is map
(called Select on LINQ), and that could help you on getting the specific fields of a query.
Its signature is [a] -> (a -> b) -> [b]
.
Its one of the most handy functions used on FP and it helps a lot when it comes to querying stuff since you can do an
var listOfField = list.Select(x => x.Field);
to easily get all fields from a list of models.
Good to know thanks, but here it was actually quite simple i just added a .Select and added everything i wanted to select.