How to take Min/Max values for adjacent columns after grouping in LINQ?
Guys I'm newbie to LINQ. Kindly help me out. Following is my scenario...
ParaId-------ErrorId
1-------------1
60------------2
125-----------3
126-----------3
127-----------3
128-----------3
129-----------3
247-----------4
248-----------4
249-----------4
Above is the result out of some query.The ErrorId column repeats values. What I want is the Min开发者_StackOverflow社区 and Max values for ParaId column for each group of ErrorId values...
For example for ErrorId 3, the Min ParaId is 125 and Max is 129.
I'm stuck at... var q = abc.GroupBy(x => x.ErrorID); // abc is the resultset
And don't know what to do next. Kindly help me sort out the above.
Thanks in advance.
This will do it
class Program
{
static void Main(string[] args)
{
List<Err> l=new List<Err>() {
new Err(1,1),
new Err(60,2),
new Err(125,3),
new Err(126,3),
new Err(127,3),
new Err(128,3),
new Err(129,3),
new Err(247,4),
new Err(248,4),
new Err(249,4)};
var x=l.GroupBy(y=>y.ErrorId).Select(y=>new {y.Key,MaxPara=y.Max(z=>z.ParaId),MinPara=y.Min(z=>z.ParaId)});
foreach(var y in x)
Console.WriteLine("{0} {1} {2}",y.Key,y.MaxPara,y.MinPara);
}
}
}
精彩评论