C# Linq Group By on multiple columns?

Easy: var consolidatedChildren = from c in children group c by new { c. School, c. Friend, c.

FavoriteColor, } into gcs select new ConsolidatedChild() { School = gcs.Key. School, Friend = gcs.Key. Friend, FavoriteColor = gcs.Key.

FavoriteColor, Children = gcs.ToList(), }.

Excellent, thank you for the answer in LINQ – Kasy Mar 8 at 12:17.

Given a list: var list = new List() { new Child() {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "John"}, new Child() {School = "School2", FavoriteColor = "blue", Friend = "Bob", Name = "Pete"}, new Child() {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "Fred"}, new Child() {School = "School2", FavoriteColor = "blue", Friend = "Fred", Name = "Bob"}, }; The query would look like: var newList = list. GroupBy(x => new {x. School, x.

Friend, x. FavoriteColor}) . Select(x => new ConsolidatedChild() { FavoriteColor = x.Key.

FavoriteColor, Friend = x.Key. Friend, School = x.Key. School, Children = x.ToList() } ); Test code: foreach(var item in newList) { Console.

WriteLine("School: {0} FavouriteColor: {1} Friend: {2}", item. School,item. FavoriteColor,item.

Friend); foreach(var child in item. Children) { Console. WriteLine("\t Name: {0}", child.Name); } } Result: School: School1 FavouriteColor: blue Friend: Bob Name: John Name: Fred School: School2 FavouriteColor: blue Friend: Bob Name: Pete School: School2 FavouriteColor: blue Friend: Fred Name: Bob.

Yes it is possible. Take a look at the sample provided in this post.

Thanks but my example is a little more specific as I add "Child" to the Children List. – Kasy Mar 8 at 11:45.

GroupBy(x => new {x. FavoriteColor = y.Key. Friend = y.Key.

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