Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
C# Linq Group By en múltiples columnas
public class ConsolidatedChild { public string School { get; set; } public string Friend { get; set; } public string FavoriteColor { get; set; } public List<Child> Children { get; set; } } public class Child { public string School { get; set; } public string Name { get; set; } public string Address { get; set; } public string Friend { get; set; } public string Mother { get; set; } public string FavoriteColor { get; set; } }

Dadas las dos clases anteriores, me gustaría usar LINQ para crear una Lista a partir de la Lista, agrupada por las propiedades Escuela, Amigo y Color favorito. ¿Es esto posible con LINQ?

Ignore las propiedades, el código se ha escrito solo para ayudar con la pregunta.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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(), };

 var consolidatedChildren = children .GroupBy(c => new { c.School, c.Friend, c.FavoriteColor, }) .Select(gcs => new ConsolidatedChild() { School = gcs.Key.School, Friend = gcs.Key.Friend, FavoriteColor = gcs.Key.FavoriteColor, Children = gcs.ToList(), });
over 4 years ago · Santiago Trujillo Report

0

Dada una lista:

 var list = new List<Child>() { 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"}, };

La consulta se vería así:

 var newList = list .GroupBy(x => new {x.School, x.Friend, x.FavoriteColor}) .Select(y => new ConsolidatedChild() { FavoriteColor = y.Key.FavoriteColor, Friend = y.Key.Friend, School = y.Key.School, Children = y.ToList() } );

Código de prueba:

 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); } }

Resultado:

 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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!