The method Distinct is not supported

眉间皱痕 提交于 2020-01-04 12:57:12

问题


I am using Linq to Entities and am getting this error

The method Distinct is not supported

On this line of code

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

Why is this?


回答1:


A solution is to define a WCF Data Service using (server-side) the QueryByCube method provided by my product AdaptiveLINQ (www.adaptivelinq.com). This method transforms a projection (expressed by the select operator) in an aggregation.

You have just to define a cube with one dimension : AttributeTypeId

av => av.AttributeTypeId

Define a Data Service providing the queryable collection:

yourDbContext.AttributeValues.QueryByCube(yourCube);

Now you can query your service using the OData protocol:

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

Or, if your using a C# client:

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

The advantage of this solution relative to the workaround proposed A J Qarshi is that the distinction is made on the server side.




回答2:


As per MSDN few LINQ methods are not supported which using OData service. Below is a partial list of unsupported methods.

  • All
  • Any
  • Concat
  • DefaultIfEmpty
  • Distinct
  • Except
  • Intersect
  • Union
  • Zip

For a full list of unsupported operations see here

However, as a workaround following statement should work in this case.

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


来源:https://stackoverflow.com/questions/13127173/the-method-distinct-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!