简体   繁体   English

实体框架中导航属性的属性总和

[英]Sum of a property of navigation property in Entity Framework

I would like to sum data displayed on the web page from a column. 我想汇总一列显示在网页上的数据。

I tried using 我尝试使用

ViewBag.Total = bookingdb.Packages.Sum(x => x.PkgBasePrice);

which gets me the sum of whats in the database obviously, 显然,这使我得到了数据库中所有内容的总和,

And I tried 我试过了

@Model.Sum(i => i.Package.PkgBasePrice)

as well where I am getting an null exception error. 以及我收到null异常错误的地方。

I have 3 tables connected, Bookings packages and customers through EF. 我通过EF连接了3张桌子,预订套餐和客户。

The view form has the parent table booking so if I try to add anything else for example 视图表单具有父表预订,因此,例如,如果我尝试添加其他任何内容

@Model.Sum(i => i.TravelerCount)

it works, and the problem is when it goes like i.table.column that gives an error. 它起作用了,问题在于它何时出现i.table.column这样的错误。

I would like to sum all PkgBasePrice data shown on the web page as those numbers are filtered through customers and are repeated so sum through database is not an option. 我想对网页上显示的所有PkgBasePrice数据求和,因为这些数字是通过客户过滤并重复的,因此通过数据库求和不是一个选择。

This is from View 这是从视图

@model IEnumerable<Online.Models.Booking>
@{
  ViewBag.Title = "Bookings";
}



@*<h1>Welcome @Session["CustomerId"].ToString()</h1>*@
<h1>Welcome @Session["Username"].ToString()</h1>

<p>
<h2> Your Bookings </h2>
</p>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.BookingDate)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.BookingNo)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.TravelerCount)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Customer.CustFirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Customer.CustLastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Package.PkgName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.PkgBasePrice)
    </th>
    <th></th>
</tr>

   @foreach (var item in Model.Where(ModelItem => 
 ModelItem.CustomerId.Equals(Session["CustomerId"])))
 {
    <tr>
        @*<td>
                @Html.DisplayFor(modelItem => item.Customer.CustomerId)
            </td>*@
        <td>
            @Html.DisplayFor(modelItem => item.BookingDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BookingNo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TravelerCount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Customer.CustFirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Customer.CustLastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Package.PkgName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Package.PkgBasePrice)
        </td>
        <td>
            @Html.ActionLink("Package Details", "Details", new { id = 
item.BookingId })
        </td>
    </tr>

}
</table>
@Model.Sum(i => i.Package.PkgBasePrice) //doesn't Work
<div> @ViewBag.Total </div>
<div>
@Html.ActionLink("Personal Information", "Customer", "Customer")
</div>

Problem is when you are loading the Booking List in the controller method its not loading the relevant Package for each Booking . 问题是,当您以控制器方法加载Booking List ,它不会为每个Booking加载相关的Package To load relevant Package for each Booking in the Booking List you can use Incldue as follows: 要在Booking List为每个Booking加载相关的Package ,您可以使用Incldue ,如下所示:

var bookingList = bookingdb.Bookings.Include(b= > b.Package).ToList();
return View(bookingList);

Moreover to ensure that a Booking must containing a Package , write your query as follows. 此外,为确保Booking必须包含Package ,请按以下方式编写查询。 Otherwise if any Booking contain no Package it will throw null reference exception during the Sum . 否则,如果任何Booking包含Package ,它将在Sum期间引发null引用异常。

@Model.Where(b => b.Package != null).Sum(i => i.Package.PkgBasePrice)

Hope it will work for you now. 希望它现在对您有用。

The first question to ask i suppose is “Where are you getting the data from? 我想问的第一个问题是“您从哪里获得数据? Are you accessing entities directly in your view?” 您是在视图中直接访问实体吗?”

If so, I'd strongly discourage that. 如果是这样,我强烈不建议这样做。 You are better off using view models and populating that view model in your business layer. 您最好使用视图模型并在业务层中填充该视图模型。 Your views should just be binding to a view model and displaying its values; 您的视图应仅绑定到视图模型并显示其值; you shouldn't be mixing logic into your view this way. 您不应以这种方式将逻辑混入您的视图中。

The second question would be “are you adding the includes for the additional navigation properties”? 第二个问题是“您是否要为其他导航属性添加包含项”? Null values can sometimes be caused by the navigation property not being included in the underlying query. 空值有时可能是由于导航属性未包含在基础查询中而引起的。

Fundamentally, I don't think there's enough information here to help fully 从根本上讲,我认为这里没有足够的信息来全面帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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