How can I write Linq/lambda expression for selecting all the records for this week.
1 week = Sunday to Saturday(inclusive).
For example if today is Wednesday I should get all the records of this week i.e from Sunday to Wednesday.
Well, I haven't wrote code completely and also not tested. I am writing it and just got stuck at "thisWeek".
[HttpPost]
public ActionResult Purchase(PurchaseSearchVM vm)
{
var filter = new PurchaseFilterRepository();
var model = filter.FilterPurchase(vm);
return View(model);
}
public IQueryable<Purchase> FilterPurchase(PurchaseSearchVM vm)
{
var result = db.Purchases.AsQueryable();
if (vm != null)
{
if (!string.IsNullOrEmpty(vm.option))
{
if (vm.option == "today")
{
result = result.Where(p => p.Date.Date == DateTime.Now.Date);
}
else if (vm.option == "yesterday")
{
result = result.Where(p => p.Date.Date == DateTime.Now.Date.AddDays(-1));
}
else if (vm.option == "thisWeek")
{
//help needed here
result = result.Where(p=> p.Date.)
}
else if (vm.option == "thisMonth") { }
else if (vm.option == "lastMonth") { }
else if (vm.option == "thisYear") { }
else if (vm.option == "lastYear") { }
}
if (!string.IsNullOrEmpty(vm.supplier))
{
var value = vm.supplier;
//query here
}
if (vm.fromDate != null || vm.toDate != null)
{
if (vm.fromDate != null && vm.toDate == null)
{
//query here
}
else if (vm.fromDate == null && vm.toDate != null)
{
//query here
}
else if (vm.fromDate != null && vm.toDate != null)
{
//query here
}
}
if (vm.IsPaid != null)
{
//query here
}
}
return result;
}
UI