codeputer
codeputer

Reputation: 2028

Cast has killed me -?

 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

Upvotes: 0

Views: 165

Answers (2)

Bryan Watts
Bryan Watts

Reputation: 45465

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);

Upvotes: 3

Jonathan Bates
Jonathan Bates

Reputation: 1835

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);

Upvotes: 0

Related Questions