不支持Distinct方法

时间:2012-10-29 18:01:07

标签: entity-framework-4 distinct wcf-data-services

我正在使用Linq to Entities并收到此错误

The method Distinct is not supported

在这行代码上

var typeIds = _context.AttributeValues.Select(av => av.AttributeTypeId).Distinct();

为什么会这样?

2 个答案:

答案 0 :(得分:1)

根据MSDN,不支持使用OData服务的LINQ方法。以下是不支持的方法的部分列表。

  • 所有
  • 任何
  • 的毗连
  • DefaultIfEmpty
  • 鲜明
  • 相交
  • 联盟
  • 邮编

有关不受支持的操作的完整列表,请参阅here

但是,在这种情况下,解决方法后面的语句应该有效。

var typeIds = _context.AttributeValues
                      .Select(av => av.AttributeTypeId)
                      .AsEnumerable()
                      .Distinct();

答案 1 :(得分:0)

解决方案是使用(服务器端)我的产品AdaptiveLINQ(www.adaptivelinq.com)提供的QueryByCube方法定义WCF数据服务。 此方法转换聚合中的投影(由选择运算符表示)。

您只需要定义一个具有一个维度的多维数据集:AttributeTypeId

av => av.AttributeTypeId

定义提供可查询集合的数据服务:

yourDbContext.AttributeValues.QueryByCube(yourCube);

现在您可以使用OData协议查询您的服务:

http://.../myService.svc/AttributeValues?$select=AttributeTypeId

或者,如果您使用的是C#客户端:

serviceContext.AttributeValues.Select(x => x.AttributeTypeId);

此解决方案相对于提议的解决方案的优势A J Qarshi是区分在服务器端