Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding IEnumerable<T> items to IEnumerable<T> [duplicate]

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?

like image 292
SVI Avatar asked Aug 27 '26 04:08

SVI


2 Answers

Could you not use Concat?

Something like

        IEnumerable<string> s1 = new List<string>
                                    {
                                        "tada"
                                    };
        IEnumerable<string> s2 = new List<string>
                                    {
                                        "Foo"
                                    };
        s1 = s1.Concat(s2);
like image 198
Adriaan Stander Avatar answered Aug 29 '26 18:08

Adriaan Stander


var result = selected.Select(item=>_repo.GetAllDCategories(item))
                     .SelectMany(x=>x,(x,cat)=> select new {
                                                   label = cat.Name, 
                                                   value = cat.Id
                                                });
like image 22
King King Avatar answered Aug 29 '26 17:08

King King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!