tags:

views:

110

answers:

2
 Error  4 Cannot implicitly convert type  
'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<CCE_2009.Data.Person>>'  
to  
 'System.Collections.Generic.IEnumerable<CCE_2009.Data.Person>'

Generated from:

var RecoveryManagerQuery =
    from RM in
    (
        from REP in e.Results
        select REP.Organization.Representatives.Select(RM=>RM.Person)
    )
    select RM;
RecoveryManagers = new ObservableCollection<Person>(RecoveryManagerQuery.ToList());

Why is this IEnumberable embeded - Organizations have one or more representatives where each representative is related to one and only one person.

I want a list of Persons that match this criteria.....

argghh..

R

+3  A: 

The outer query is redundant. We can make this clearer by removing it:

var RecoveryManagerQuery =
    from REP in e.Results
    select REP.Organization.Representatives.Select(RM=>RM.Person);

As you can see, the select clause says, for each REP, to select all the people associated with representatives in REP's organization. This means that each element in RecoveryManagerQuery is going to be a set of Person objects, not an individual object. You want to flatten the query so it returns a set of Person objects, not a set of a set of Person objects:

var RecoveryManagerQuery =
    from REP in e.Results
    from orgRep in REP.Organization.Representatives
    select orgRep.Person;

Edit: Here is the dot notation:

e.Results.SelectMany(
    REP => REP.Organization.Representatives,
    (REP, orgRep) => orgRep.Person);
Bryan Watts
I seein your example their is only one select, and hence, only one collection. How would you write this in dot notation?var RecoveryManagerQuery = e.Results.Select(P => P.Organization.Representatives.Select(RM=>RM.Person)); but this too has two selects and generates the same error.. just curious at this point in time!
codputer
I got it to work this way - looks much more complicated than I think it should be:e.Results.Select(P=>P.Organization.Representatives).SelectMany(Reps=>Reps.ToArray()).Select(Rep=>Rep.Person);
codputer
@codputer: I added the dot notation to my answer.
Bryan Watts
Never used SelectMany - but I think I know now how and when to use it. Thanks for taking the time!
codputer
A: 

What are you trying to achieve? You are getting a IEnumerable of an IEnumerable because of the nested LINQ query.

Depending upon what you are trying to achieve, you could change your query to:

var RecoveryManagerQuery =
    from REP in e.Results
    select REP.Organization.Representatives.Select(RM=>RM.Person);
Jonathan Bates
I tested this and it still returned an IEnumerable within an IEnumberable... which I think now makes sense to me as their are two selects hence the two collections... see answer to avoid the double select
codputer