Linq GroupBy and multiple columns?

In your particular case, you could use x.First(). Team inside your Select to get your team information.

In your particular case, you could use x.First(). Team inside your Select to get your team information. For other cases of actually needing to group on multiple fields, you could group on an anonymous type.

Such as someQuery. GroupBy(f => new { f. Foo, f.

Bar }). Select(...).

1 Good answer pointing out that he could use First for his problem. – Tomas Jansson Dec 14 '10 at 13:13 thank you, excellent answer – fearofawhackplanet Dec 14 '10 at 13:15.

Query expression version of it (might be easier to read, depending on situation and amount of grouping): IEnumerable GetEmployeeExpenses(List claims) { return from c in claims group c by c. EmployeeId into groupedById from g in groupedById group g by g. Team into groupedByTeam let firstElement = groupedByTeam.First() select new EmployeeExpense { EmployeeId = firstElement.

EmployeeId, Team = firstElement. Team, Cost = groupedByTeam. Sum(e => e.

Cost) }; }.

Not exacaty sure what you're asking but if you want to group by multiple columns use the ThenBy syntax: _expenseClaims. GroupBy(e => e. EmployeeId).

ThenBy(e => e. Team).

That's what I thought it was, but intellisense isn't giving me a ThenBy. Note _expenseClaims is a List (I'll add that to the question) – fearofawhackplanet Dec 14 '10 at 13:05 ThenBy is used for sorting, following an original OrderBy. – Anthony Pegram Dec 14 '10 at 13:07 You're totally right.... thought it existed for GroupBy also.

– Tomas Jansson Dec 14 '10 at 13:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions