c# - 使用 LINQ 将字段从 Listitem 转换为 List<T>

标签 c# linq sharepoint

我正在使用 CAML 查询 Sharepoint Online 数据库并获取它返回的必需 ListItemCollection。我只对此 ListItemCollection 的一个字段感兴趣,并将其转换为 List 以供另一个函数使用。有没有办法使用 LINQ 和 lambda 函数来做到这一点?

我当前的代码运行良好,如下所示(listItems 是来自 Sharepoint 客户端命名空间的 ListItemCollection 类型)。

List<string> theList = new List<string>();

foreach(ListItem itm in listItems)  //listItems is type ListItemCollection
{
   theList.Add(itm["Email_x0020_Address"].ToString());
}

看起来应该是这样的

theList = listItems.ToList<string>(  i => i["Email_x0020_Address"].ToString()  );

theList = listItems.Cast<ListItem>().ToList<string>(i =>
               i["Email_x0020_Address"].ToString());

但我在转换时遇到了问题。任何帮助,将不胜感激!谢谢!

最佳答案

使用 Select 将一个序列中的每个项目投影到另一个序列中(然后可以将其添加到列表中):

var emails = items.Select(item => (string)item["Email_x0020_Address"])
    .ToList();

关于c# - 使用 LINQ 将字段从 Listitem 转换为 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30131694/

相关文章:

c# - 具有构造函数的 ServiceStack 服务类

c# - 将 GridView 绑定(bind)到列表

c# - 如果给定父元素下存在某个元素,则仅解析 xml 文件

c# - Sql to Linq 难点——group by, having

css - 在 Sharepoint webpart 中使用自定义 css 作为嵌入式资源不起作用,为什么?

ruby - 通过 Ruby Controller 在 SharePoint 上使用 Web 服务

c# - 将Linq中的NULL和字符串连接到实体查询

c# - 在 Java 或 C# 中提高编程效率是一个神话吗?

c# - LINQ 中两个列表的并集不起作用

c# - ASP.Net 如何从 gridview C# 更新数据库