2014-09-04 64 views
0

我試圖使用LINQ和SPListItem集合中的SharePoint查找字段的值 - 是這樣的:SPFieldLookupValue在LINQ查詢

int totalDepts = (from SPListItem itm in hourEntries select ((SPFieldLookupValue)itm["Level1"]).LookupValue).Distinct().Count(); 

但是,這似乎並沒有工作(和它罷工我缺少一些步驟)

有沒有人做過這個?

+0

根據經驗,Linq to SharePOint有點危險,很難定製。我的建議是創建純CAML查詢,事件如果語法有點複雜。最重要的是,使用CAML查詢,您將確保您的查詢將由Sharepoint處理,而不是在內存中處理。 – 2014-09-04 13:17:13

回答

0

我無法弄清楚直接在LINQ查詢做到這一點,所以我結束了創建WorkHoursEntries對象,並且用我所有的SPListItems

List<WorkHourEntry> workHourEntries = new List<WorkHourEntry>(); 
       foreach (SPListItem hourEntry in hourItems) 
       { 
        //Collect entries that are in the current Fiscal Year reporting period 
        if (fiscalYearMonths.Contains(hourEntry["Reporting_x0020_Month"].ToString())) 
        { 

        WorkHourEntry curEntry = new WorkHourEntry(); 
        string Level1 = (string)hourEntry["Level1"]; 
        SPFieldLookupValue val = new SPFieldLookupValue(Level1); 
        curEntry.organization = val.LookupValue; 
        SPFieldCalculated cf = (SPFieldCalculated)hourEntry.Fields["WECSCHours"]; 
        curEntry.WECSCHours = cf.GetFieldValueForEdit(hourEntry["WECSCHours"]); 
        workHourEntries.Add(curEntry); 

        } 
       } 

這讓我跑LINQ查詢填充它直接在WorkHourEntry集合

var uniqueDeptNames = (from itm in workHourEntries select itm.organization).Distinct().ToArray();