C#Linq两个日期之间的日期

时间:2010-02-10 14:25:20

标签: c# linq

我正在尝试获取我的linq语句,以便在两个日期之间获取所有记录,而且我不太确定需要更改才能使其工作:(a.Start >= startDate && endDate)

var appointmentNoShow =
    from a in appointments
    from p in properties
    from c in clients
    where a.Id == p.OID && (a.Start.Date >= startDate.Date && endDate)

7 个答案:

答案 0 :(得分:129)

只需将其更改为

即可
var appointmentNoShow = from a in appointments
                        from p in properties
                        from c in clients
                        where a.Id == p.OID && 
                       (a.Start.Date >= startDate.Date && a.Start.Date <= endDate)

答案 1 :(得分:22)

var appointmentNoShow = from a in appointments
                        from p in properties
                        from c in clients
                        where a.Id == p.OID
                        where a.Start.Date >= startDate.Date
                        where a.Start.Date <= endDate.Date

答案 2 :(得分:3)

var QueryNew = _context.Appointments.Include(x => x.Employee).Include(x => x.city).Where(x => x.CreatedOn >= FromDate).Where(x => x.CreatedOn <= ToDate).Where(x => x.IsActive == true).ToList();

答案 3 :(得分:1)

如果具有日期间隔过滤条件,则需要选择部分属于此过滤范围的所有记录。假设:记录具有ValidFrom和ValidTo属性。

DateTime intervalDateFrom = new DateTime(1990, 01, 01);
DateTime intervalDateTo = new DateTime(2000, 01, 01);

var itemsFiltered = allItems.Where(x=> 
    (x.ValidFrom >= intervalDateFrom && x.ValidFrom <= intervalDateTo) ||
    (x.ValidTo >= intervalDateFrom && x.ValidTo <= intervalDateTo) ||
    (intervalDateFrom >= x.ValidFrom && intervalDateFrom <= x.ValidTo) ||
    (intervalDateTo >= x.ValidFrom && intervalDateTo <= x.ValidTo)
);

答案 4 :(得分:0)

&#13;
&#13;
 public List<tbltask> gettaskssdata(int? c, int? userid, string a, string StartDate, string EndDate, int? ProjectID, int? statusid)
        {
            List<tbltask> tbtask = new List<tbltask>();
            DateTime sdate = (StartDate != "") ? Convert.ToDateTime(StartDate).Date : new DateTime();
            DateTime edate = (EndDate != "") ? Convert.ToDateTime(EndDate).Date : new DateTime();
            tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).
                Where(x => x.tblproject.company_id == c
                    && (ProjectID == 0 || ProjectID == x.tblproject.ProjectId)
                    && (statusid == 0 || statusid == x.tblstatu.StatusId)
                    && (a == "" || (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a)))
                    && ((StartDate == "" && EndDate == "") || ((x.StartDate >= sdate && x.EndDate <= edate)))).ToList();



            return tbtask;


        }
&#13;
&#13;
&#13;

这是我对基于searchdata的搜索记录的查询以及从开始到结束日期之间的查询

答案 5 :(得分:0)

所以您正在向下滚动,因为答案不起作用:

这就像魔术(但他们说这对大数据的效率问题,而你不在乎我一样)

1-在我的情况下,数据库中的数据类型为“ datetime”和“ nullable”。

在实施例的DB数据的格式是这样的:

2018-11-06 15:33:43.640

在C#中,当转换为字符串时,就像:

2019-01-03 4:45:16 PM

因此,格式为:

yyyy/MM/dd hh:mm:ss tt

2-因此,您需要首先以正确的格式准备datetime变量:

示例1

yourDate.ToString("yyyy/MM/dd hh:mm:ss tt")

示例2-最近30天的日期时间范围

    DateTime dateStart = DateTime.Now.AddDays(-30);
    DateTime dateEnd = DateTime.Now.AddDays(1).AddTicks(-1);

3-最终,您失去了试图找到的linq查询(需要EF 6)

using System.Data.Entity;

_dbContext.Shipments.Where(s => (DbFunctions.TruncateTime(s.Created_at.Value) >= dateStart && DbFunctions.TruncateTime(s.Created_at.Value) <= dateEnd)).Count();

还要考虑时间比较:

(DbFunctions.CreateDateTime(s.Created_at.Value.Year, s.Created_at.Value.Month, s.Created_at.Value.Day, s.Created_at.Value.Hour, s.Created_at.Value.Minute, s.Created_at.Value.Second) >= dateStart && DbFunctions.CreateDateTime(s.Created_at.Value.Year, s.Created_at.Value.Month, s.Created_at.Value.Day, s.Created_at.Value.Hour, s.Created_at.Value.Minute, s.Created_at.Value.Second) <= dateEnd)

请注意在其他stackoverflow问题和答案中提到的以下方法将无法正常工作:

....
&&
(
    s.Created_at.Value.Day >= dateStart.Day && s.Created_at.Value.Day <= dateEnd.Day &&
    s.Created_at.Value.Month >= dateStart.Month && s.Created_at.Value.Month <= dateEnd.Month &&
    s.Created_at.Value.Year >= dateStart.Year && s.Created_at.Value.Year <= dateEnd.Year
)).count();

例如,如果开始日期在本月,而结束日期在下个月,则查询将返回false并且没有结果,例如:

DatabaseCreatedAtItemThatWeWant = 2018/12/05

的startDate = 2018年12月1日

EndDate = 2019/01/04

查询将始终搜索01到04之间的天,而不考虑“月”,因此“ s.Created_at.Value.Day <= dateEnd.Day”将失败

如果您有非常大的数据,则可以执行本机SQL查询而不是linq

...
    ... where Shipments.Created_at BETWEEN CAST(@Created_at_from as datetime) AND CAST(@Created_at_to as datetime))
    ....

谢谢

答案 6 :(得分:0)

我无法使它正常工作。

我在db行中有两个日期,我需要将它们添加到昨天,今天和明天的列表中。

这是我的解决方案:

        var yesterday = DateTime.Today.AddDays(-1);
        var today = DateTime.Today;
        var tomorrow = DateTime.Today.AddDays(1);            
        var vm = new Model()
        {
            Yesterday = _context.Table.Where(x => x.From <= yesterday && x.To >= yesterday).ToList(),
            Today = _context.Table.Where(x => x.From <= today & x.To >= today).ToList(),
            Tomorrow = _context.Table.Where(x => x.From <= tomorrow & x.To >= tomorrow).ToList()
        };