C# lambda表达式的实体框架组不工作?
我在.NETMVC上工作了好几天。我想显示一个表,在这个表中,我向视图发送一些列和分组的总和。我面临的问题是=>行动C# lambda表达式的实体框架组不工作?,c#,entity-framework,asp.net-mvc-4,lambda,C#,Entity Framework,Asp.net Mvc 4,Lambda,我在.NETMVC上工作了好几天。我想显示一个表,在这个表中,我向视图发送一些列和分组的总和。我面临的问题是=>行动 public ActionResult Index() { TempData["UserDetails"] = db.UserDetails.Include(x => x.User).ToList(); var financeofstudents = db.Financeofstudents.Include(f => f
public ActionResult Index()
{
TempData["UserDetails"] = db.UserDetails.Include(x => x.User).ToList();
var financeofstudents = db.Financeofstudents.Include(f => f.Batche).Include(f => f.User).GroupBy(x => x.UserId).Select(x => new
{
debit = x.Sum(item => item.debit),
credit = x.Sum(item => item.credit),
balance = x.Sum(item => item.balance)
}).ToList();
return View(financeofstudents);
}
耶!也许它的工作很好,但我不知道。因为我在客户端遇到了几个错误
当我只是在客户端发送一个列表及其工作文件时。like=>
var financeofstudents = db.Financeofstudents.Include(f => f.Batche).Include(f => f.User).ToList();
但是当我使用这个=>时,因为我需要一个group by==>
var financeofstudents = db.Financeofstudents.Include(f => f.Batche).Include(f => f.User).GroupBy(x => x.UserId).Select(x => new
{
debit = x.Sum(item => item.debit),
credit = x.Sum(item => item.credit),
balance = x.Sum(item => item.balance)
}).ToList();
我得到的错误是=>
而我的观点=>
@using TSMS_Atp_2_Final_Project.Models.Com.Tsms
@model IEnumerable<Financeofstudent>
@{
ViewBag.Title = "Finance";
IEnumerable<UserDetail> UserDetailsTable = TempData["UserDetails"] as IEnumerable<UserDetail>;
}
<h2>Available Finance Detail Of Students...</h2>
<p>
<a href="@Url.Action("ASIB", "Batche")" class="btn btn-primary btn-sm">Assign New</a>
</p>
<div class="table-responsive">
<table class="table table-striped table-inverse table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.Batche.batch_code)
</th>
<th>
@Html.DisplayNameFor(model => model.Batche.name)
</th>
<th>
@Html.DisplayNameFor(model => model.User.UserId)
</th>
<th>
@Html.Label("Full Name")
</th>
<th>
@Html.DisplayNameFor(model => model.debit)
</th>
<th>
@Html.DisplayNameFor(model => model.credit)
</th>
<th>
@Html.DisplayNameFor(model => model.balance)
</th>
<th>
@Html.DisplayNameFor(model => model.lastTrunsaction)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.Batche.batch_code, "Details", "Batche", new { id = item.Batche.batch_code }, null)
</td>
<td>
@Html.ActionLink(item.Batche.name, "Details", "Course", new { id = item.Batche.name }, null)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserId)
</td>
<td>
@{
var UserDetailsId = UserDetailsTable.Where(x => x.UserId == item.User.UserId).Select(x => x.id).FirstOrDefault();
var FullName = UserDetailsTable.Where(x => x.UserId == item.User.UserId).Select(x => x.fullname).FirstOrDefault();
}
@Html.ActionLink(FullName, "Details", "Students", new { id = UserDetailsId }, null)
</td>
<td>
@Html.DisplayFor(modelItem => item.debit)
</td>
<td>
@Html.DisplayFor(modelItem => item.credit)
</td>
<td>
@Html.DisplayFor(modelItem => item.balance)
</td>
<td>
@Html.DisplayFor(modelItem => item.lastTrunsaction)
</td>
<td>
<div style="width:80px;">
@Html.ActionLink("Edit", "Edit", new { id = item.id }) |
@Html.ActionLink("Details", "Details", new { id = item.id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.id })
</div>
</td>
</tr>
}
</table>
</div>
在客户端,如上表所示?请帮助我。我该如何解决我的问题。或者我做错了什么。或者有其他方法可以获得我想要的输出。您的group by只返回3个整数的列表。该列表是passend,因为它们是您视图的模型。您无权访问任何数据库对象属性 基本上,执行的SQL只获取3个和的列表…看看下面的代码:
.Select(x => new
{
debit = x.Sum(item => item.debit),
credit = x.Sum(item => item.credit),
balance = x.Sum(item => item.balance)
}).ToList();
它返回匿名类型,但您的视图使用IEnumerableclass FinanceofstudentsModel
{
public string UserId { get; set; }
public int Debit { get; set; }
public int Credit { get; set; }
public int Balance { get; set; }
}
然后将您的选择更改为类似以下内容
var financeofstudents = db.Financeofstudents
.Include(f => f.Batche)
.Include(f => f.User)
.GroupBy(x => x.UserId)
.Select(x => new FinanceofstudentsModel()
{
UserId = x.Key,
Debit = x.Sum(item => item.debit),
Credit = x.Sum(item => item.credit),
Balance = x.Sum(item => item.balance)
});
在您的视图中使用此模型
@model IEnumerable<FinanceofstudentModel>
您的视图可能如下所示:
@model IEnumerable<FinanceofstudentModel>
@foreach (var fm in Model)
{
<p>User: @fm.UserId Debit: @fm.Debit Credit: @fm.Credit Balance: @fm.Balance</p>
<hr/>
}
因此,重要的是,您的模型只有4个属性UserId、Debit、Credit和Balance。若你们想添加额外的属性,你们应该在FinanceofstudentModel中添加它并填写controller
我希望这能帮助您创建一个类:
public class StudentFinance
{
public double debit { get; set; }
public double credit { get; set; }
public double balance { get; set; }
}
将LINQ查询更改为
var financeofstudents = db.Financeofstudents.Include(f => f.Batche).Include(f => f.User).GroupBy(x => x.UserId).Select(x => new StudentFinance
{
debit = x.Sum(item => item.debit),
credit = x.Sum(item => item.credit),
balance = x.Sum(item => item.balance)
}).ToList();
最后,将视图上的模型更改为:
@model IEnumerable<StudentFinance>
那应该行得通。。如果这解决了您的问题,请标记已回答。您正在创建匿名对象的集合,并将其传递给需要Financeofstudent集合的视图-它们需要是相同的类型。创建一个视图模型,其中包含您想要在视图中使用的属性。选择X=>new yourViewModel{…,然后在视图中使用该视图模型
@model IEnumerable<FinanceofstudentModel>
@foreach (var fm in Model)
{
<p>User: @fm.UserId Debit: @fm.Debit Credit: @fm.Credit Balance: @fm.Balance</p>
<hr/>
}
public class StudentFinance
{
public double debit { get; set; }
public double credit { get; set; }
public double balance { get; set; }
}
var financeofstudents = db.Financeofstudents.Include(f => f.Batche).Include(f => f.User).GroupBy(x => x.UserId).Select(x => new StudentFinance
{
debit = x.Sum(item => item.debit),
credit = x.Sum(item => item.credit),
balance = x.Sum(item => item.balance)
}).ToList();
@model IEnumerable<StudentFinance>