简体   繁体   English

如何从SharePoint CSOM查询中获取唯一字段值?

[英]How can I get only unique field values from a SharePoint CSOM query?

I'm using the SharePoint client object model to retrieve data from a SharePoint 2013 List. 我正在使用SharePoint客户端对象模型从SharePoint 2013列表中检索数据。 The following code works, and gets me all values from the keyCol List field, correctly sorted: 以下代码有效,并从keyCol List字段中正确地排序了所有值:

string keyCol = "SPFieldName";
CamlQuery keyColumnValuesQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection keyColumnValues = srcDocLib.GetItems(keyColumnValuesQuery);
spContext.Load(keyColumnValues, items => items.Include(item => item[keyCol]), items => items.OrderBy(item => item[keyCol]));
spContext.ExecuteQuery();

However, I would only like to retrieve just one Item for every unique value in the field, a Distinct. 但是,我只想为字段中的每个唯一值检索一个项,即唯一项。 When I add the Distinct() Linq/Lambda to the Load method retrievals argument (no compile time issues): 当我将Distinct()Linq / Lambda添加到Load方法的检索参数时(没有编译时问题):

spContext.Load(keyColumnValues, items => items.Include(item => item[keyCol]), items => items.OrderBy(item => item[keyCol]), items => items.Distinct());

I get an InvalidQueryExpressionException: the query expression 'items.Distinct()' is not supported. 我收到一个InvalidQueryExpressionException:查询表达式'items.Distinct()'不支持。

Does this mean the Linq provider on the SharePoint side doesn't support that expression and I'm just out of luck using this technique? 这是否意味着SharePoint方面的Linq提供程序不支持该表达式,而我对使用这种技术感到不满意? Am I doing something incorrectly? 我做错了什么吗? Thanks. 谢谢。

Linq provider for SharePoint does not support Distinct operator. 用于SharePoint的Linq提供程序不支持Distinct运算符。

According to MSDN: 根据MSDN:

Some LINQ queries cannot be completely translated into CAML. 某些LINQ查询不能完全转换为CAML。 However, however such queries can, in principle, run correctly because they can be executed in two stages. 但是,这些查询原则上可以正确运行,因为它们可以分两个阶段执行。 First, the LINQ to SharePoint provider translates as much of the query into CAML as it can and executes that query 首先,LINQ to SharePoint提供程序将尽可能多的查询转换为CAML并执行该查询

Please refer Unsupported LINQ Queries and Two-stage Queries for a more details. 有关更多详细信息,请参考不支持的LINQ查询和两阶段查询

Two stage approach 两阶段方法

To correct this error, you should cut your queries in two stages to force the first query execution before the second one. 要更正此错误,您应该将查询分为两个阶段,以强制第一个查询先于第二个查询执行。 To do that, you should for example transform the first IEnumerable<T> in a list thanks to ToList() method. 为此,例如,应使用ToList()方法转换列表中的第一个IEnumerable<T>

The following example demonstrates how to return unique values : 下面的示例演示如何返回唯一值:

   //1 Execute query against SP Linq provider   
    string keyCol = "SPFieldName";
    CamlQuery keyColumnValuesQuery = CamlQuery.CreateAllItemsQuery();
    ListItemCollection keyColumnValues = srcDocLib.GetItems(keyColumnValuesQuery);
    spContext.Load(keyColumnValues, items => items.Include(item => item[keyCol]), items => items.OrderBy(item => item[keyCol]));
    spContext.ExecuteQuery();
    //2. Transform LiItemCollection to a List and perform Distinct operation 
    var uniqueKeyColumnValues = keyColumnValues.ToList().Select(i => i[keyCol]).Distinct(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM