[英]Sequence contains no elements Error
My dbstructure is as follows 我的数据库结构如下
StudentRegistrationTable 学生注册表
Id Name
5 Sachin
StudentReceiptTable 学生收据表
Id StudRegId Fee ST Total Status DueDate
1 5 873 127 1000 1 01/05/2016
2 5 873 127 2000 1 01/15/2016
3 5 873 127 3000 0 01/25/2016
4 5 873 127 4000 0 01/28/2016
5 5 873 127 5000 0 01/30/2016
Status indicates the payment mode.Status 1 indicates student has paid the receipt and 0 indicates the unpaid receipt 状态表示付款方式。状态1表示学生已付款,而0表示未付款。
Query 询问
_dTableReg = _db.StudentRegistrations
.AsEnumerable()
.Where(r => (..condition))
.Select(r => new RegistraionVM.RegDataTable
{
...
...
NextDueAmount = r.StudentReceipts.
Where(rc => rc.Status == false)
.First().Total.ToString(),
NextDueDate = r.StudentReceipts.
Where(rc => rc.Status == false)
.First().DueDate.Date.ToString('dd/MM/yyyy')
}).OrderByDescending(r => r.RegistrationID).ToList();
The above query returns the first unpaid amount and date(3000 & 01/25/2016). 上面的查询返回第一个未付金额和日期(3000和01/25/2016)。
The problem arises when student has paid all the receipt (ie status will be set to 1) and i am getting Sequence contains no elements errror
.In that case I want to return FULL PAID
in both NextDueAmount
and NexDueDate
当学生已付清所有收据(即状态将设置为1)并且我正在获取
Sequence contains no elements errror
, Sequence contains no elements errror
。在这种情况下,我想同时在NextDueAmount
和NexDueDate
返回FULL PAID
RegDataTable Class RegDataTable类
public class RegDataTable
{
...
...
public string NextDueAmount { get; set; }
public string NextDueDate { get; set; }
}
Your use of .First()
will throw the error is the collection of StudentReceipt
returns no items (ie when the Status
of all items is true
). 您对
.First()
将引发错误, StudentReceipt
的集合StudentReceipt
返回任何项目(即,当所有项目的Status
为true
)。 You need to use .FirstOrDefault()
and then check if the value is null
, and if not then access the Total
and DueDate
properties. 您需要使用
.FirstOrDefault()
,然后检查该值是否为null
,如果不是,则访问Total
和DueDate
属性。
This could make you controller code unnecessarily complex (and your also accessing the database twice to get the collection) so I suggest you use a view model (if its not already) with additional read only properties to return the results 这可能使您的控制器代码不必要地复杂(并且您也两次访问数据库以获取集合),所以我建议您使用具有附加只读属性的视图模型(如果尚未使用)以返回结果
public class RegDataTableVM
{
....
public StudentReceipt Receipt { get; set; }
public string NextDueAmount
{
get { return Receipt == null ? "FULL PAID" ? Receipt.Total.ToString() }
}
public string NextDueDate
{
get { return Receipt == null ? "FULL PAID" ? Receipt.DueDate.ToString("dd/MM/yyyy") }
}
and modify the query to 并将查询修改为
_dTableReg = _db.StudentRegistrations
.Where(r => (..condition))
.Select(r => new RegDataTableVM
{
...
...
Receipt = r.StudentReceipts.Where(rc => rc.Status == false).FirstOrDefault()
}).OrderByDescending(r => r.RegistrationID).ToList();
Side note: If your using DisplayFor()
to generate the html, you can also make use of the DisplayFormatAttribute
旁注:如果您使用
DisplayFor()
生成html,则还可以使用DisplayFormatAttribute
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", NullDisplayText = "FULL PAID"
public DateTime? NextDueDate
{
get { return return Receipt == null ? null ? Receipt.DueDate }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.