c# - Get Min and Max for items from database using Linq -
in database have :
name value first player 300 first player 500 first player 250 secondplayer 200 second player 450
and need show each player max , min values:
name maxvalue minvalue firstplayer 500 250 secondplayer 450 200
i did this:
var res = (from item in _context.players select item).max(s=>s.value);
but 1 player max value;
query syntax:
var result = (from item in _context.players group item name @group select new { name = @group.key, maxvalue = @group.max(i=>i.value), minvalue = @group.min(i=>i.value) }).tolist();
or in nice linq methods syntax: (this overload lets .select()
"in" .groupby()
var result = _context.players.groupby(item => item.name, (key, @group) => new { name = key, maxvalue = @group.max(i => i.value), minvalue = @group.min(i => i.value) }).tolist();
Comments
Post a Comment