1

Closed

Unable to cast the type - Regression EF5 to EF6

description

We running into the following exception:
An exception of type 'System.NotSupportedException' occurred in MyCompany.Core.dll but was not handled in user code

Additional information: Unable to cast the type 'System.Linq.IQueryable`1[[<>f__AnonymousType3`6[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MyCompany.MyProduct.Prc.Management.Server, Version=5.0.0.0, Culture=neutral, PublicKeyToken=fc353342480d063d]]' to type 'System.Data.Entity.Core.Objects.ObjectQuery`1[[<>f__AnonymousType3`6[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MyCompany.MyProduct.Prc.Management.Server, Version=5.0.0.0, Culture=neutral, PublicKeyToken=fc353342480d063d]]'. LINQ to Entities only supports casting EDM primitive or enumeration types.
The Query works fine with Entity Framework 5 but leads to an exception in Entity Framework 6.0.1:
var query0 = from price in dbContext.Set<MyPrice>()
                           where price.Identity.MyDate == MyDate
                           select new
                           {
                             AssetIdentityId = price.Identity.AssetIdentityId,
                             MyMethod = price.Identity.MyMethod,
                             MyDate = price.Identity.MyDate,
                             Price = price
                           };

var query1= from price in dbContext.Set<MyPrice>()
                                   where price.Identity.MyDate < MyDate
                                   select new
                                   {
                                     Id = price.Id,
                                     AssetIdentityId = price.Identity.AssetIdentityId,
                                     MyMethod = price.Identity.MyMethod,
                                     MyDate = price.Identity.MyDate,
                                     ProviderPriceDate = price.ProviderPriceDate,
                                     Value = price.Value,
                                   };

var query2=        from priceData in query0 
        let assetIdentityId = priceData.AssetIdentityId
        let MyMethod = priceData.MyMethod
        let maxPreviousReferenceDate = query1
          .Where(gp => gp.MyMethod == MyMethod && gp.AssetIdentityId == assetIdentityId)
          .Max(gp => gp.MyDate)
        select new
        {
          Price = priceData,
          PreviousPriceId = query1= 
            .Where(ppd => ppd.MyMethod == MyMethod && ppd.AssetIdentityId == assetIdentityId && ppd.MyDate == maxPreviousReferenceDate)
            .Max(p => p.Id),
        };
Execution of query0 or query1 works. Execution of query2 leads to the mentioned exception.
Closed Nov 7, 2013 at 9:56 PM by maumar

comments

moozzyk wrote Nov 6, 2013 at 4:18 PM

maumar wrote Nov 7, 2013 at 9:55 PM

Verified that this is a dupe of issue 1751, and that it no longer repros on current build, closing

BGrojer wrote Nov 8, 2013 at 4:26 AM

Thank you guys. The duplicate looks a little bit different because it also happens without anonymous types. Will try to check that with a nightly build.