开发者

Convert Linq ObjectQuery IQueryable to IEnumerable

Totally confused about the data types required here.

I have this Linq statement:

                var feat = AllCustomers
                    .Select(c => c.CustomerServices.SelectMany(c开发者_开发技巧s => 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


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)


System.Data.Objects.ObjectQuery<T> is an implementation of System.Linq.IQueryable<T>. Your type, however, is an IQueryable of IEnumerable objects. Perform a SelectMany, so you no longer have a collection of string collections, but just a collection of strings.


Depending on what you need to do with the list you can just do:

var itemTypes = feat.ToList();


you can use

List<string> list = feat.ToList<string>()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜