c# - 两个日期之间的 Lambda 显式

标签 c# sql asp.net linq lambda

我需要一些向导。

我有一张 table

Start                 End                   PersonID
-----------------------------------------------------
10/07/2017 00:00:00   18/07/2017 00:00:00   1
27/07/2017 00:00:00   27/07/2017 00:00:00   1
28/07/2017 00:00:00   28/07/2017 00:00:00   1
29/07/2017 00:00:00   29/07/2017 00:00:00   1
30/07/2017 00:00:00   30/07/2017 00:00:00   1

如果我搜索

Date Start = 11/07/2017
Date End = 12/07/2017 

使用此查询:

DateTime start = new DateTime(2017,07,11,0,0,0,0,0);
DateTime end = start.AddDays(1);
DateTime[] days = new DateTime[end.Subtract(start).Days];

for (int i = 0; i < end.Subtract(start).Days; i++)
{
     var d = start.AddDays(i);
     days[i] = d;
}

IQueryable block = tmOpen1.Calendar.Where(x => days.All(y => y >= x.start && y <= x.end)).Select(x => new { ID = x.PersonID });`

我在第 1 行得到了阳性结果(2017 年 7 月 10 日 - 2017 年 7 月 18 日)

但是,如果我将其应用于其余行,例如过滤器

Date Start = 28/07/2017
Date End = 29/07/2017

那么显然这会失败。我怎样才能让这方面的搜索工作。

例如要么

  1. 获取第一行并将其分成单独的行
  2. 如果某个人有多个 true 条件,则使各个行返回 true。

我希望这里的一位天才可以提供帮助。

最佳答案

似乎您真正需要的是这样的东西:

DateTime start = new DateTime(2017,07,11,0,0,0,0,0);
DateTime end = start.AddDays(1);

var results = tmOpen1.Calendar
    .Where(c => start <= c.end && end >= c.start)
    .Select(x => new { ID = x.PersonID });

关于c# - 两个日期之间的 Lambda 显式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351736/

相关文章:

sql - Apache Impala的迭代函数

SQL 多 CASE 语句返回 0 而不是 NULL

c# - 将数据表转换为私有(private) List<List<string>>?

asp.net - 在后面的代码中检查单选按钮时出现问题

c# - 列表订阅操作 : 403 forbidden

c# - 如何在ASP.NET MVC编辑页面中为HttpPostedFileBase创建输入类型文件?

c# - 网络共享上的 .NET 4.0 应用程序导致 SecurityException

c# - 如何在分页期间将 ASPxTextBoxes 的值存储在 DevExpress GridView DataItemTemplate 中

sql - 如果数据流中的某些文件不存在,如何成功运行数据流任务

asp.net - 将 Dictionary<string,string> 加入查询字符串的最快方法是什么?