简体   繁体   English

这个 LINQ 查询有什么问题?

[英]What is wrong with this LINQ query?

I am trying to write a LINQ query against two objects ( SPListItemCollection and List<SPListItem> ).我正在尝试针对两个对象( SPListItemCollectionList<SPListItem> )编写 LINQ 查询。

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:当我向 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当前上下文中不存在名称“z”

How can I select z.ID ?我怎么能 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 .在您的第二个版本中,您需要稍微更改语法以获得具有 2 个属性的匿名类型,即nParentID

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM