C#-无法在LINQ to Entities查询中构造实体或复杂类型

C#-无法在LINQ to Entities查询中构造实体或复杂类型,c#,asp.net-mvc,entity-framework,linq,C#,Asp.net Mvc,Entity Framework,Linq,我试图用视图发送数据并将其打印出来,但由于我是C#的新手,所以我真的很努力 这是我的模型视图模型: namespace P104.Models { public class ViewModel { } public class Location { public int loc_id { get; set; } public string loc_name { get; set; } } public c

我试图用视图发送数据并将其打印出来,但由于我是C#的新手,所以我真的很努力

这是我的模型视图模型:

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; }
    }
}
@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>
}
他就是我如何在视图中打印出来的:

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; }
    }
}
@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>
}
@foreach(Model.Locations中的变量位置)
{
@location.locname
@location.locname
}
@foreach(模型中的var能力)
{
@竞争,竞争
@竞争,竞争
}
我总是收到这个错误

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);
}
实体或复杂类型“P104.Models.Locations”不能为空 在LINQ to实体查询中构造

我已经找到了一些解决方案,但我正在努力将它们应用到我的代码中,以使它们不起作用。
提前感谢您的帮助

您似乎在这里输入了一个错误:

select new Locations { loc_name = loc.loc_name };
这应该是:

select new Location { loc_name = loc.loc_name };
您投影到的模型称为
位置
,而不是
位置
。现在还不清楚
位置是什么
模型,因为您在问题中没有说明这一点

当然,也要相应地调整你的观点:

@foreach (var location in Model.Locations)
{
    <dt>@location.loc_name</dt>
    <dd>@location.loc_name</dd>
}
@foreach(Model.Locations中的变量位置)
{
@location.loc_名称
@location.loc_名称
}

顺便说一下,我建议您遵循标准的C#命名约定。此约定规定,在C中,属性名称应以大写字母开头,且不包含
。因此,基本上你宁愿使用
LocationName
或只是
Name
而不是
locu Name
。对于您的
Competite
模型,也有同样的评论。

突然,我得到了这个错误:无效的对象名“dbo.UserComps”。数据库名为“dbo.UserComp”,我不知道他是从哪里得到的。如果不知道
dbo
在您的上下文或
UserComp
中是什么,那么很难提供帮助。我建议你问一个新问题,请尽量具体一点。仅显示代码的相关部分。在本例中,这将是EF DataContext定义和LINQ查询。