Adding IEnumerable<T> items to IEnumerable<T>
c#, ienumerable, linq
Solution
Well, I think there is some confusion here,
var result = selected.SelectMany(item =>
_repo.GetAllDCategories(item).Select(cat =>
new
{
Label = cat.Name,
Value = cat.Id
});
seems to me what you want.
You can use `SelectMany` to "squash" or "flatten" an `IEnumerable<IEnumerable<T>>` into an `IEnumerable<T>`.
Its similar to having a function like this
IEnumerable<KeyValuePair<string, int>> GetSelectedCategories(
IEnumerable<string> selected)
{
foreach (var item in selected)
{
foreach (var category in _repo.GetAllDCategories(item))
{
yield return new KeyValuePair<string, int>(
category.Name,
category.Id);
}
}
}
Problem
I have the following: ``` foreach (var item in selected) { var categories = _repo.GetAllDCategories(item); var result = from cat in categories select new { label = cat.Name, value = cat.Id }; } ``` The method `GetAllDCategories` returns a `IEnumerable<T>` How to add `result` to new `IEnumerable` object that will contain all the items from `result` for all the selected items in the loop?