SharePoint linq查询从列表中检索特定列

时间:2013-05-19 12:39:11

标签: c# linq sharepoint

我使用SharePoint客户端对象模型查询Sharepoint中的列表,并需要在帮助程序集中动态创建Linq查询。

如何从列表< string>创建以下内容?列名称包含" Id","标题"," LinkFileName","状态" ?

context.Load(listItems, items => items.Include(  
  item => item["Id"],  
  item => item["Title"],  
  item => item["LinkFilename"],  
  item => item["Status"]  
));     

1 个答案:

答案 0 :(得分:4)

非常感谢Manvir解决我问题的帖子:

http://www.manvir.net/invalidqueryexpressionexception-the-query-expression-is-not-supported/

ClientContext.Load替换为支持运行时构建表达式的ClientContext.LoadQuery

private static Expression<Func<ListItem, object>>[] CreateListItemLoadExpressions(List<string> viewFields)
{
   List<Expression<Func<ListItem, object>>> expressions = new List<Expression<Func<ListItem, object>>>();
   foreach (string viewFieldEntry in viewFields)
   {
      string fieldInternalName = viewFieldEntry;
      Expression<Func<ListItem, object>> retrieveFieldDataExpression = listItem => listItem[fieldInternalName];
      expressions.Add(retrieveFieldDataExpression);
   }
   return expressions.ToArray();
}