问题
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