Linq查询中的SPFieldLookupValue
我正在尝试使用Linq和Linq查询中的SPFieldLookupValue,linq,sharepoint,Linq,Sharepoint,我正在尝试使用Linq和SPListItem的集合来计算SharePoint中查找字段的值,如下所示: int totalDepts = (from SPListItem itm in hourEntries select ((SPFieldLookupValue)itm["Level1"]).LookupValue).Distinct().Count(); 但这似乎不起作用(我觉得它遗漏了一些步骤) 以前有人这样做过吗?我无法在Linq查询中直接执行此操作,因此我最终创建了Workho
SPListItem
的集合来计算SharePoint中查找字段的值,如下所示:
int totalDepts = (from SPListItem itm in hourEntries select ((SPFieldLookupValue)itm["Level1"]).LookupValue).Distinct().Count();
但这似乎不起作用(我觉得它遗漏了一些步骤)
以前有人这样做过吗?我无法在Linq查询中直接执行此操作,因此我最终创建了WorkhourEntries对象,并用我的所有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到SharePOint有点危险,而且很难定制。我的建议是创建纯CAML查询,如果语法有点复杂的话。最重要的是,使用CAML查询,您将确保您的查询将由Sharepoint处理,而不是在内存中。
var uniqueDeptNames = (from itm in workHourEntries select itm.organization).Distinct().ToArray();