2011-05-25 125 views
1

我想寫兩個對象的LINQ查詢(SPListItemCollectionList<SPListItem>)。這個LINQ查詢有什麼問題?

當我查詢像它下面的一個正常工作:

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; 

當我添加一個項目選擇:

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; 

它開始出錯了:

當前上下文中不存在名稱'z'

如何選擇z.ID

+0

您的意思是'選擇新{的OBJ = N,的ParentId = z.ID}'? – 2011-05-25 20:14:38

+0

你有沒有試過選擇新{n,ParentId = z.ID} – 2011-05-25 20:14:47

回答

6

在第二個版本中,您需要稍微更改語法以獲得具有2個屬性的nParentID的匿名類型。

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

如果這不是你想要的,請澄清的問題。

0

您的最終查詢應該是這樣的一個

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