如何编写Linq / lambda表达式来选择本周的所有记录。
1周=周日至周六(含)。
例如,如果今天是星期三,我应该得到本周的所有记录,即从星期日到星期三。
好吧,我还没有完全编写代码,也没有经过测试。我正在写它,只是被卡在“ 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
答案 0 :(得分:1)
您可以像这样计算startDayOfWeek
和endDayOfWeek
: -
DateTime startDayOfWeek= DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek));
DateTime endDayOfWeek = DateTime.Today.AddDays(6 - (int)DateTime.Today.DayOfWeek);
然后只需使用Where
应用过滤器: -
var result = vm.Where(x => x.myDate >= startDayOfWeek && x.myDate <= endDayOfWeek );
答案 1 :(得分:0)
请尝试使用以下代码段。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Purchase> dates = new List<Purchase>();
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(-6) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(-5) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(-4) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(-3) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(-2) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(-1) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(0) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(1) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(2) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(7) });
dates.Add(new Purchase() { FromDate = DateTime.Now.AddDays(8) });
var test = dates.Where(i => i.FromDate >= DateTime.Now.StartOfWeek(DayOfWeek.Sunday) && i.FromDate <= DateTime.Now.StartOfWeek(DayOfWeek.Sunday).AddDays(7)).ToList();
// you can access the this weeks dates here
Console.ReadLine();
}
}
public class Purchase
{
public DateTime FromDate { get; set; }
}
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
}
}
答案 2 :(得分:0)
您可以按如下方式编写它,以便在本周开始之前删除几天:
result = result.Where(
p=> p.Date >= p.Date.AddDays(-(p.Date.DayOfWeek - DayOfWeek.Sunday) &&
p.Date <= DateTime.Today.Date);
或全球化:
var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
result = result.Where(
p=> p.Date >= p.Date.AddDays(-(p.Date.DayOfWeek - firstDayOfWeek) &&
p.Date <= DateTime.Today.Date);