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
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).
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
RegDataTable Class
public class RegDataTable
{
...
...
public string NextDueAmount { get; set; }
public string NextDueDate { get; set; }
}
.First()will return that error if there are no items in the collection. Use.FirstOrDefault();, then check fornull, then if not, access the properties - user3559349