如何在视图中使用groupby数据以C#,MVC,LINQ显示结果 [英] How to use groupby data in view to display a result, in C#, MVC, LINQ
本文介绍了如何在视图中使用groupby数据以C#,MVC,LINQ显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
你能帮我看看如何为每个用户显示总支出
UserRecords表
+ ------- + ------ + -------------- +
| Id |名称|成本|
+ ------- + ------ + -------------- +
| 1 |安娜| 30 。 3 |
+ ------- + ------ + -------------- +
| 2 |安娜| 500 |
+ ------- + ------ + -------------- +
| 3 |山姆| 30 。 3 |
+ ------- + ------ + -------------- +
| 4 |汤姆| 20 。 5 |
+ ------- + ------ + -------------- +
| 5 |汤姆| 20 。 5 |
+ ------- + ------ + -------------- +
| 6 |安娜| 20 。 5 |
+ ------- + ------ + -------------- +
欲望结果如何在网页中显示 MVC
Anna 550 。 8
Sam 30 。 3
Tom 41
总计 622 。 1
谢谢;)
我的尝试:
public ActionResult Index()
{
// 显示用户和费用
var userNameGroup = db.UserRecords.GroupBy(c => c.Name);
foreach ( var group in userNameGroup)
{
var result =( {0} - {1}, group .Key, group .Max(s = > s.Cost));
}
return 查看(db.UserRecords.GroupBy(x => x.UserNames)。ToList());
}
// 如何从此处转到网页显示
@ model IEnumerable < MobileReports.Models.UserRecord >
< 表 class = table >
@if(!Model.Any())
{
@ Html.LabelForModel(尚未上传数据)
}
else
{
< tr >
< th >
@ Html.DisplayNameFor(model => model.Cost)
< / th >
< th >
@ Html.DisplayNameFor(model => model.Name)
< / th >
< th > < / th >
< / tr >
foreach(模型中的var项)
{
< tr >
< td >
@ Html.DisplayFor(modelItem => item.Cost)
< / td >
< td >
@ Html.DisplayFor(modelItem => item.Name)
< / td >
< td >
@ Html.DisplayFor(modelItem = > item.Total)
< / td >
< / tr >
}
}
< / table >
解决方案
您的视图希望模型为IEnumerable< UserRecord>
,但您的操作是传递IEnumerable< IGrouping< string ,UserRecord>>
。你需要让类型匹配。
一个简单的选择是在视图中对记录进行分组:
public ActionResult Index()
{
return 查看(db。 UserRecords.ToList());
}
@ model IEnumerable< MobileReports.Models.UserRecord>
@ if (!Model.Any())
{
< p > 尚未上传任何数据。< ; / p >
}
else
{
< 表格 >
< thead >
< tr > ;
< th 范围 = col > 名称< / th >
< th 范围 = col > 费用< / th >
< / tr >
< / thead >
< tfoot >
< tr >
< th 范围 = row > 总计:< / th >
< td > @ Html .DisplayFor(m = > m.Sum(u = > u.Cost))< / td >
< tr >
< / tfoot >
< tbody >
@ foreach ( var 项 Model.GroupBy(u = > u.Name,(key,items)= > new {Name = key,Cost = items.Sum(u = > u.Cost)}))
{
< tr >
< th 范围 = row > @Html .DisplayFor(_ = > item.Name)< / < span class =code-leadattribute> th >
< th scope = row > @Html .DisplayFor(_ = > item.Cost)< / th >
< / tr >
}
< / tbody >
< / table >
}
更简洁的方法是创建一个视图模型来表示分组结果。在操作中填充视图模型,并更改视图以使用视图模型。
public class UserCostsViewModel
{
public string 名称{ get ; set ; }
public decimal 费用{ get 跨度>; set ; }
}
public class GroupedUserCostsViewModel
{
public IReadOnlyList< UserCostsViewModel>项目{获取; set ; }
public decimal 总计{ get 跨度>; set ; }
}
public ActionResult Index()
{
< span class =code-keyword> var source = db.UserRecords.ToList();
var items = source
.GroupBy(u = > u.Name,(key,items)= > new UserCostsViewModel
{
Name = key,
Cost = items.Sum(u = > u.Cost)
})
.ToList ();
var model = new GroupedUserCostsViewModel
{
Items = items,
Total = source.Sum(u = > u.Cost)
};
return 查看(模型);
}
@ model GroupedUserCostsViewModel
@if (!Model.Items.Any())
{
< p > 尚未上传任何数据。< / p >
}
else
{
< table >
< thead >
< tr >
< th 范围 = col > 名称< / th >
< th 范围 = col > 费用< / th >
< / tr >
< / thead >
< tfoot >
< tr >
< th scope = row > 总计:< ; / th >
< td > @ Html .DisplayFor(m = > m.Total)< / td >
< tr >
< / tfoot >
< tbody >
@ foreach ( var item in Model.Items)
{
< span class =code-keyword>< tr >
< th 范围 = row > @ Html .DisplayFor(_ = > item.Name)< / th >
< th 范围 = row > @ Html .DisplayFor(_ = > item.Cost)< / th >
< / tr >
}
< / tbody >
< / table >
}
我建议创建另一个模型TotalUserData
public class TotalUserData
{
public 字符串名称
{
get ;
set ;
}
public double TotalCost
{
get ;
set ;
}
}
和以上型号的控制器。
然后......
public ActionResult Index()
{
//显示用户和成本
var userNameGroup = db.UserRecords
.GroupBy(c => c.Name)
.Select(grp => new TotalUserData(Name = grp.Key,Total = grp.Sum(x) => x.Cost)))
.ToList();
返回View(userNameGroup);
}
最后,更改查看
以便能够显示相应的数据。
@ model IEnumerable < MvcApplication1.Models.TotalUserData >
@ {
ViewBag.Title =总;
}
< h2 > 总< / h2 >
< 表 class = table >
@if(!Model.Any())
{
@ Html.LabelForModel(尚未上传数据)
}
其他
{
< tr >
< >
@ Html.DisplayNameFor(model => mod el.Name)
< / th >
< th >
@ Html.DisplayNameFor(model => model.TotalCost)
< / th >
@ * < th > 操作 < / th < span class =code-keyword>> * @
< / tr >
foreach(模型中的var项目)
{
< tr >
< td >
@ Html.DisplayFor( modelItem => item.Name)
< / td >
< td >
@Html .DisplayFor(modelItem => item.TotalCost)
< / td >
< / tr >
}
< tr > < td colspan = 2 > 总计:@ Model.Sum(x => x.TotalCost)< ; / td > < / tr >
}
< / table > < /跨度>
Hi all
Could you please help me how to display for each user the total spending
UserRecords table
+-------+------+--------------+
|Id | Name | Cost |
+-------+------+--------------+
| 1 | Anna | 30.3 |
+-------+------+--------------+
| 2 | Anna | 500 |
+-------+------+--------------+
| 3 | Sam | 30.3 |
+-------+------+--------------+
| 4 | Tom | 20.5 |
+-------+------+--------------+
| 5 | Tom | 20.5 |
+-------+------+--------------+
| 6 | Anna | 20.5 |
+-------+------+--------------+
desire result how to display in web page MVC
Anna 550.8
Sam 30.3
Tom 41
Total 622.1
Thank you ;)
What I have tried:
public ActionResult Index()
{
//Disply user and cost
var userNameGroup = db.UserRecords.GroupBy(c=>c.Name);
foreach (var group in userNameGroup)
{
var result = ("{0} -{1}", group.Key, group.Max(s => s.Cost));
}
return View(db.UserRecords.GroupBy(x=>x.UserNames).ToList());
}
//how to go from this to the webpage display
@model IEnumerable<MobileReports.Models.UserRecord>
<table class="table">
@if (!Model.Any())
{
@Html.LabelForModel("Not data uploaded yet")
}
else
{
<tr>
<th>
@Html.DisplayNameFor(model => model.Cost)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Cost)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Total)
</td>
</tr>
}
}
</table>
解决方案
Your view expects the model to beIEnumerable<UserRecord>
, but your action is passing anIEnumerable<IGrouping<string, UserRecord>>
. You need to make the types match.
One simple option would be to group the records in the view:
public ActionResult Index() { return View(db.UserRecords.ToList()); }@model IEnumerable<MobileReports.Models.UserRecord> @if (!Model.Any()) { <p>No data uploaded yet.</p> } else { <table> <thead> <tr> <th scope="col">Name</th> <th scope="col">Cost</th> </tr> </thead> <tfoot> <tr> <th scope="row">Total:</th> <td>@Html.DisplayFor(m => m.Sum(u => u.Cost))</td> <tr> </tfoot> <tbody> @foreach (var item in Model.GroupBy(u => u.Name, (key, items) => new { Name = key, Cost = items.Sum(u => u.Cost) })) { <tr> <th scope="row">@Html.DisplayFor(_ => item.Name)</th> <th scope="row">@Html.DisplayFor(_ => item.Cost)</th> </tr> } </tbody> </table> }
A cleaner approach would be to create a view-model to represent the grouped results. Populate the view-model in the action, and change the view to use the view-model.
public class UserCostsViewModel { public string Name { get; set; } public decimal Cost { get; set; } } public class GroupedUserCostsViewModel { public IReadOnlyList<UserCostsViewModel> Items { get; set; } public decimal Total { get; set; } }public ActionResult Index() { var source = db.UserRecords.ToList(); var items = source .GroupBy(u => u.Name, (key, items) => new UserCostsViewModel { Name = key, Cost = items.Sum(u => u.Cost) }) .ToList(); var model = new GroupedUserCostsViewModel { Items = items, Total = source.Sum(u => u.Cost) }; return View(model); }@model GroupedUserCostsViewModel @if (!Model.Items.Any()) { <p>No data uploaded yet.</p> } else { <table> <thead> <tr> <th scope="col">Name</th> <th scope="col">Cost</th> </tr> </thead> <tfoot> <tr> <th scope="row">Total:</th> <td>@Html.DisplayFor(m => m.Total)</td> <tr> </tfoot> <tbody> @foreach (var item in Model.Items) { <tr> <th scope="row">@Html.DisplayFor(_ => item.Name)</th> <th scope="row">@Html.DisplayFor(_ => item.Cost)</th> </tr> } </tbody> </table> }
I'd suggest to create another modelTotalUserData
public class TotalUserData { public string Name { get; set; } public double TotalCost { get; set; } }
and a controller for above model.
Then...
public ActionResult Index() { //Display user and cost var userNameGroup = db.UserRecords .GroupBy(c=>c.Name) .Select(grp=> new TotalUserData(Name=grp.Key, Total=grp.Sum(x=>x.Cost))) .ToList(); return View(userNameGroup); }
Finally, changeView
to be able to display corresponding data.
@model IEnumerable<MvcApplication1.Models.TotalUserData> @{ ViewBag.Title = "Total"; } <h2>Total</h2> <table class="table"> @if (!Model.Any()) { @Html.LabelForModel("Not data uploaded yet") } else { <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.TotalCost) </th> @*<th>Action</th>*@ </tr> foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.TotalCost) </td> </tr> } <tr><td colspan="2">Total of total: @Model.Sum(x=>x.TotalCost)</td></tr> } </table>
这篇关于如何在视图中使用groupby数据以C#,MVC,LINQ显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文