user2769810
user2769810

Reputation: 155

LINQ: Add to list for each item

I am trying to convert the following line to LINQ, what is the recommended way to do so? I have the following code:

IEnumerable<person> result = Enumerable.Empty<person>();
var list1 = getlist1(..)
var repository = new Repository();
for(int i = 0; i < list1.Length; i++)
{
    result = result.Union(repository.GetList(list1[i].ID));
}

I want to call repository.GetList for each item in list1, and add the results to a list.

Upvotes: 0

Views: 744

Answers (1)

stuartd
stuartd

Reputation: 73243

IEnumerable<person> result = list1.SelectMany(i => repository.GetList(i.ID));

And if you want them as a list:

 IList<person> result = list1.SelectMany(i => repository.GetList(i.ID)).ToList();

Upvotes: 2

Related Questions