从Sharepoint列表中读取时使用where子句进行Foreach



我正在尝试筛选从Sharepoint(列表(读取的结果集。我必须筛选有用户指定的记录,遗憾的是,列表中的用户列是一个查找列,并从另一个列表中获取实际用户名。

通常,我可以得到一个记录的用户名值(如果有(,如下所示:

foreach (var item in listItems)
{
if (item.FieldValues.TryGetValue("nqhs", out var userLookup))
{
if (userLookup != null && userLookup.ToString() == "Microsoft.SharePoint.Client.FieldUserValue")
{
var theUser = ((FieldLookupValue)item["nqhs"]).LookupValue; //this returns theUser = "John Doe"
}
}
}

nchs是Sharepoint列表中包含用户名的列的名称

有没有什么方法可以将这个逻辑应用于foreach中的where子句,这样我就可以使用更小的结果集?

我正在尝试这样的东西,但我无法让它发挥作用:

foreach (var item in listItems.Where(p => p.((FieldLookupValue)p["nqhs"]).LookupValue == "John Doe"))

也尝试过这个,得到和";对象引用未设置为对象的实例";错误

foreach (var item in from item in listItems where ((FieldLookupValue)item["nqhs"]).LookupValue == "John Doe" select item)

首先尝试过滤查找列表,然后在每个中使用过滤后的列表

var filteredList = listItems.Where(p => p.((FieldLookupValue)p["nqhs"]);
foreach(var item in filteredList)

对SP的某些查询需要执行才能返回结果,然后才能使用这些查询。它对性能也更好,因为当前循环中的Where子句将在每次迭代中执行。

最新更新