从 Sharepoint 列表读取时带有 where 子句的 Foreach

Foreach with where clause when reading from a Sharepoint List

我正在尝试过滤从 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"
        }
    }
}

nqhs 是 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)

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

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

foreach(var item in filteredList)

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