简体   繁体   中英

C# - The entity or complex type cannot be constructed in a LINQ to Entities query

I'm trying to send data with a view and print it out, but I'm struggling really hard since I'm very new to C#.

So here's my model ViewModel:

namespace P104.Models
{
    public class ViewModel
    {
    }

    public class Location
    {
        public int loc_id { get; set; }
        public string loc_name { get; set; }
    }

    public class Competentie
    {
        public int Comp_id { get; set; }
        public string competentie { get; set; }
    }

    public class MyViewModel
    {
        public User User { get; set; }
        public IEnumerable<Locations> Locations { get; set; }
        public IEnumerable<Competenties> Competenties { get; set; }
    }
}

This is the function I have in the controller

public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    User user = db.user.Find(id);

    if (user == null)
    {
        return HttpNotFound();
    }

    var competenties =
        from usercomp in dbE.UserComp
        join comp in dbE.Competenties on usercomp.fk_Comp_id equals comp.comp_id
        where usercomp.fk_user_id == id
        select new Competenties { competentie = comp.competentie };

    var locations =
        from userloc in dbE.UserLoc
        join loc in dbE.Locations on userloc.fk_location_id equals loc.loc_id
        where userloc.fk_user_id == id
        select new Locations { loc_name = loc.loc_name };

    var model = new MyViewModel
    {
        User = user,
        Locations = locations.ToList(), // eagerly fetch the data that will be needed in the view
        Competenties = competenties.ToList(), // eagerly fetch the data that will be needed in the view
    };
    return View(model);
}

And he's how I try to print it out in the view:

@foreach (var location in Model.Locations)
{
    <dt>@location.locname</dt>
    <dd>@location.locname</dd>
}
@foreach (var competentie in Model.Competenties)
{
    <dt>@competentie.competentie</dt>
    <dd>@competentie.competentie</dd>
}

I always recevie this error

The entity or complex type 'P104.Models.Locations' cannot be constructed in a LINQ to Entities query.

I've found a few solutions, but I'm struggling to apply them to my code so they don't work. Thanks in advance for your help

You seem to have got a typo here:

select new Locations { loc_name = loc.loc_name };

This should be:

select new Location { loc_name = loc.loc_name };

The model you are projecting against is called Location , not Locations . It's unclear what the Locations model is since you haven't shown that in your question.

And of course adapt your view accordingly:

@foreach (var location in Model.Locations)
{
    <dt>@location.loc_name</dt>
    <dd>@location.loc_name</dd>
}

By the way I will recommend you following standard C# naming conventions. This convention dictates that in C# a property name should start with an uppercase letter and not contain _ . So basically you would rather use LocationName or just Name instead of loc_name . Same remark for your Competentie model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM