Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Split collection
Message
De
17/02/2011 11:40:32
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
À
17/02/2011 11:27:04
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01500522
Message ID:
01500550
Vues:
48
>>>>I'm sure there's an easy way to do this but I can't come up with it...
>>>>
>>>>I have a collection with a Group string field. What I'd like to do is split that collection into (n) number of collections for each Group. I'll end up with a loop similar to this:
>>>>
>>>>
>>>>foreach (var group in groups)
>>>>{
>>>>   foreach (var item in group)
>>>>   {
>>>>      //process item.
>>>>   }
>>>>}
>>>>
>>>>
>>>>Any guidance out there??? Thanks!
>>>
>>>I think it would depend on the type of the collections. Can you be a bit more specific?
>>>Best,
>>>Viv
>>
>>Given this:
>>
>>           var people = new List<Person>();
>>            people.Add(new Person { FirstName = "John", LastName = "Doe" });
>>            people.Add(new Person { FirstName = "Jane", LastName = "Doe" });
>>            people.Add(new Person { FirstName = "Bob", LastName = "Barker" });
>>            people.Add(new Person { FirstName = "Billy", LastName = "Barker" });
>>            people.Add(new Person { FirstName = "Billy", LastName = "Bob" });
>>
>>
>>I'd like to get a collection with 3 elements. Element 1 will be a collection of people (the Does), Element 2 will be a collection of people (The Barkers), Element 3 will be a collection with Billy Bob.
>
>Maybe something like:
var v = from g in people.AsEnumerable() group g by g.LastName into r select r;
>
>foreach (var xx in v)
>   {
>         Console.WriteLine("LastName:{0}",xx.Key);
>         foreach (Person p in xx)
>              {
>                  Console.WriteLine("\t{0}",p.FirstName);
>              }
>    }
Ah, yep, the GroupBy Lambda was the key. I was initially looking at the result of that incorrectly.

Thanks!!!
Very fitting: http://xkcd.com/386/
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform