Abe Miessler
Abe Miessler

Reputation: 85096

What is wrong with this LINQ query?

I am trying to write a LINQ query against two objects ( SPListItemCollection and List<SPListItem>).

When my query is like the one below it works fine:

var licFirst = from n in navList.Items.Cast<SPListItem>()
               from z in licZeroth
               where ((SPFieldLookupValueCollection)n["Parent"]).Select(t=>t.LookupId).Contains(z.ID)
               select n;

When I add an item to the select:

var licFirst = from n in navList.Items.Cast<SPListItem>()
               from z in licZeroth
               where ((SPFieldLookupValueCollection)n["Parent"]).Select(t=>t.LookupId).Contains(z.ID)
               select n, ParentId = z.ID;

It begins to error out with:

The name 'z' does not exist in the current context

How can I select z.ID?

Upvotes: 1

Views: 389

Answers (2)

k-dev
k-dev

Reputation: 1657

your final query should be this one

var licFirst = from n in navList.Items.Cast<SPListItem>()
           from z in licZeroth
           where ((SPFieldLookupValueCollection)n["Parent"]).Select(t=>t.LookupId).Contains(z.ID)
           select new { n, ParentId = z.ID };

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126932

In your second version, you need to change the syntax a little to get an anonymous type with 2 properties, the n and the ParentID.

select new { n, ParentID = z.ID }; 

If this is not what you want, please clarify in the question.

Upvotes: 6

Related Questions