I need to fetch Previous Month Invoice from database for a particular user based on his/her relationship number. I have come up with below linq query.
// step 1: fetching Bill number based on relationship number and OrderByDescending clause
**
var query = context.Billings
.OrderByDescending(c => c.BillGenerationDate)
.Where(c => c.CAFNO == caf)
.Select(c => c.BillNo)
.FirstOrDefault();
** //step 2 : step 1 result have the bill number which was the last(or highest) filtered by //BillGeneration date
var bill = (from b in context.Billings
where b.BillNo == query
select b).FirstOrDefault();
step 2 will result the full bill information required.
OR
Other way :
var myresult = (from c in context.Billings
where c.CAFNO == caf
orderby c.BillGenerationDate descending
select c).FirstOrDefault();
I feel the above query can be re-written in better way. Looking for suggestion to re-write the above query in more efficient manner.
Thanks !!!