C# 如何使用linq c MVC将icollection计数包括到模型中

C# 如何使用linq c MVC将icollection计数包括到模型中,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有以下型号: public class Person { public int PersonID {get;set;} public string Name {get;set;} public string City {get;set;} public ICollection<Room> Rooms {get;set;} } public class Room { public int RoomID {get;set;} publi

我有以下型号:

public class Person
{
    public int PersonID {get;set;}
    public string Name {get;set;}
    public string City {get;set;}
    public ICollection<Room> Rooms {get;set;}
}

public class Room
{
    public int RoomID {get;set;}
    public bool Decorated {get; set;}
    public int PersonID {get;set;}
    public Person Person {get;set;}
}
但是我得到了这个错误

包含路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,选择运算符作为集合导航属性

我是新手!非常感谢您的帮助。

您不能只将计数计入您的实体。Include方法允许您在从数据库检索数据时添加导航属性。这意味着您应该指向指向另一个实体的属性。在您的情况下,它将是:

db.Persons.Include(x => x.Rooms)
它将加载所有引用Person实体的房间。然后你可以得到这样的房间数:

var roomsCount = db.Persons.Include(x => x.Rooms)
    .Where(x => x.PersonID == 12 && x.City == "New York")
    .SelectMany(x => x.Rooms)
    .Count();

这可以是使用视图模型的解决方案之一

创建视图模型

public class PersonWIthRoomCountViewModel
    {
        public string Name { get; set; }
        public string City { get; set; }
        public int DecoratedRoomsAllocated { get; set; }
    }
控制员:

public class DefaultController : Controller
    {
        protected StackOverflowAnswersContext db = new StackOverflowAnswersContext();

        public ActionResult Details(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            Person person = db.Persons.Find(id);
            if (person == null)
                return HttpNotFound();

            PersonWIthRoomCountViewModel model = new PersonWIthRoomCountViewModel();

            model.Name = person.Name;
            model.City = person.City;
            var DecoratedRooms = db.Rooms.Where(ww => ww.PersonID == id && ww.Decorated);
            if (DecoratedRooms == null)
            {
                model.DecoratedRoomsAllocated = 0;
            }
            else
            {
                model.DecoratedRoomsAllocated = DecoratedRooms.Count();
            }

            return View(model);
        }
    }
观点:

@model StackOverflowAnswers.Controllers.PersonWIthRoomCountViewModel

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Person Room Count</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.City)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.City)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.DecoratedRoomsAllocated)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.DecoratedRoomsAllocated)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
</p>
@model StackOverflowAnswers.Controllers.PersonWIthRoomCountViewModel

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Person Room Count</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.City)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.City)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.DecoratedRoomsAllocated)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.DecoratedRoomsAllocated)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
</p>