The method Distinct is not supported
Asked Answered
M

2

2

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?

Mincemeat answered 29/10, 2012 at 18:1 Comment(3)
Are you using SQL Server or another database with its own LINQ-to-Entities provider?Hofmann
Or is this WCF data services?Potboiler
I am using SQL Server and WCF data services.Mincemeat
S
0

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.

Sightless answered 19/7, 2014 at 14:37 Comment(1)
I've approved your edit to the question only after checking your answer. For future reference, it's always better if you can provide a useful description as to why the tag is needed and why does it make sense.Gilpin
D
1

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();
Diao answered 17/7, 2014 at 11:30 Comment(0)
S
0

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.

Sightless answered 19/7, 2014 at 14:37 Comment(1)
I've approved your edit to the question only after checking your answer. For future reference, it's always better if you can provide a useful description as to why the tag is needed and why does it make sense.Gilpin

© 2022 - 2024 — McMap. All rights reserved.