Linq查询中的SPFieldLookupValue

时间:2014-09-04 13:09:45

标签: linq sharepoint

我正在尝试使用Linq和SPListItem的集合在SharePoint中查找字段的值 - 这样的事情:

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

但这似乎不起作用(并且因为缺少一些步骤而让我感到震惊)

以前有人这样做过吗?

1 个答案:

答案 0 :(得分: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);

                    }
                }

这允许我直接在WorkHourEntry集合

上运行Linq查询
var uniqueDeptNames = (from itm in workHourEntries select itm.organization).Distinct().ToArray();
相关问题