foreach (var eventaricle in eventarticles) { subarticles.addrange(eventaricle.getlinkedsubarticles()); } as linq query in csharp

You can use the SelectMany LINQ extension method to achieve the same behavior as the foreach loop. Here is how to do it:

main.cs
List<EventArticle> eventarticles = new List<EventArticle>();
List<SubArticle> subarticles = eventarticles.SelectMany(eventarticle => eventarticle.GetLinkedSubArticles()).ToList();
180 chars
3 lines

The SelectMany method flattens a sequence of sequences into a single sequence. It takes a function that returns a sequence for each element in the input sequence, and concatenates all the resulting sequences into a single sequence.

In this case, the function returns the linked subarticles for each EventArticle, and ToList is used to create a list from the resulting sequence.

gistlibby LogSnag