There are simular questions with answers that do not work in my situation.
I’m getting a
Unable to create a constant value of type ‘.Model.featureoptions’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.
Using Entity First, EntityFramework 4.1, MVC3, C# 4.
vehicles is a table of vehicle details, owners is a table of vehicle owners. vehicles and owners are inner joined and that works.
features table is a list of optional features e.g. sunroof, paint, etc. featureOptions is a list of the available options for a feature. e.g. paint could be ‘pearlescent’, ‘matalic’ and sunroof could be ‘glass pop-up’, ‘title + slide’.
vehicleFeatures is a list of chosen options for a vehicle, for a particulare feature a vehicle can have zero or one record.
In this query feature1 should be null or the chosen value for a feature (i.e. the chosen sunroof option) and feature2 should be null or the chosen value for a different feature (i.e. the chosen paint option)
var query = (from v in _entities.vehicles
join o
in _entities.owners
on v.OwnerID equals o.OwnerID
// Some more inner joins
select new
{
// <code snipped >
// o. fields and v. fields
// </ code snipped>
feature1 = (from feature1
in _entities.vehiclefeatures
.Where ( f_1 => f_1.VehicleID == v.VehicleID)
join feature1_fo
in _entities.featureoptions
on feature1.FeatureOptionID equals feature1_fo.FeatureOptionID
join feature1_f
in _entities.features
.Where (bt_f => bt_f.CodeEnum==1)
on feature1_fo.FeatureID equals feature1_f.FeatureID
select new featureoptionsDTO () { Option = feature1_fo.Option }
),
feature2 = (from feature2
in _entities.vehiclefeatures
.Where(f_2 => f_2.VehicleID == v.VehicleID)
join feature2_fo
in _entities.featureoptions
on feature2.FeatureOptionID equals feature2_fo.FeatureOptionID
join feature2_f
in _entities.features
.Where(feature2_f => feature2_f.CodeEnum == 2)
on feature2_fo.FeatureID equals feature2_f.FeatureID
select new featureoptionsDTO() { Option = feature2_fo.Option }
)
}
);
foreach (var vehicle in query) // Exception here
{
}
the
feature1 = (from ..
and
feature2 = (from ..
are causing the
Unable to create a constant value of type ‘.Model.featureoptions’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.
I understand that LINQ is trying to create an entity, how can I get it to create an anonymous (or own class) instead?
Unfortunately Entity Framework can’t handle select clauses which construct arbitrary types in LINQ to Entities queries. I’ve tripped over this one a few times myself, and it’s quite annoying. It is, however, quite necessary as LINQ to Entities queries are translated into SQL to run on the database, and the database can’t handle the construction of .NET objects. It might be nice to be able to do it at the end of the query, but it can certainly never be allowed in the middle.
What I tend to do is write a query which produces exactly the input required to the constructors all in LINQ to Entities, so that it runs on the database. Then call ToEnumerable() on the IQueryable you get from that, which turns it into an IEnumerable, and after that you’re in LINQ to Objects so you can do whatever you like in your Select().