Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6935077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:03:18+00:00 2026-05-27T12:03:18+00:00

There are simular questions with answers that do not work in my situation. I’m

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:03:19+00:00Added an answer on May 27, 2026 at 12:03 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've noticed that there are a couple of similar questions and answers at SO
There are similar questions, but none answers what I really need. I actually don't
There maybe some simlar questions to this but I can't see anything that really
There are many similar questions, but apparently no perfect match, that's why I'm asking.
There might be similar questions but I still have some parts that I couldn't
First off I apologize if there is another post out there that answers this,
I know that there are plenty of similar questions on stack overflow. But the
I want to create radio buttons or other type of controls that will act
I know there's already tons of similar questions, but I checked over twenty answers
I realize that there have been similar questions, but they seem to have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.