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<开发者_如何学Go;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
?
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.
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 };
精彩评论