Search code examples
entity-framework-4distinctwcf-data-services

The method Distinct is not supported


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?


Solution

  • 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.