Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Linq ObjectQuery IQueryable to IEnumerable

Tags:

linq

Totally confused about the data types required here.

I have this Linq statement:

                var feat = AllCustomers
                    .Select(c => c.CustomerServices.SelectMany(cs => cs.CustomerServiceFeatures)
                    .SelectMany(csf => csf.ConfigElements).Where(ce => ce.Name == "ItemType").Select(ce => ce.Value).Distinct());

It returns the required data, and VS tells me that the type is being set as:

System.Data.Objects.ObjectQuery<System.Collections.Generic.IEnumerable<string>>

However I want to add this data into a list of strings:

 List<string> itemTypes = new List<string>();
 itemTypes.AddRange(feat);

But this throws an error:

 Argument 1: cannot convert from 'System.Linq.IQueryable<System.Collections.Generic.IEnumerable<string>>' to 'System.Collections.Generic.IEnumerable<string>'   

I can't find the required syntax to cast to correct type. Can anyone help?

Cheers, Matt

like image 268
Bob Tway Avatar asked Dec 01 '10 12:12

Bob Tway


1 Answers

Compile-time error shows, that feat is "collection of collection of string" (literally "IQueryable of IEnumerable of string"). So you need to generate flat collection.

feat.SelectMany(x => x)
like image 170
The Smallest Avatar answered Jan 01 '23 00:01

The Smallest