Linq查询基于startdate和enddate的获取数据

时间:2015-11-25 05:38:35

标签: linq linq-to-sql

我的表格数据在

之下

选择团队:

        fromdate    todate      IsWaiting 
72  54  11/26/2015  11/30/2015  True     
73  54  12/2/2015   12/5/2015   True    
74  54  11/26/2015  11/27/2015  False   

现在我想根据 fromdate todate

获取日期

假设

  startdate= "2015-12-03";
  enddate= "2015-12-07";

OR

  startdate= "2015-12-01";
  enddate= "2015-12-07";

然后我想要结果只有第二行

我试过了

 from ct in db.ChooseTeams
    where ct.IsWaiting == true &&
           ((startdate>= ct.FromDate && startdate<= ct.ToDate) || (enddate<= ct.ToDate && enddate>= ct.ToDate))                                             select ct

 from ct in db.ChooseTeams
      where ct.IsWaiting == true &&
      ((startdate>= ct.FromDate && enddate<= ct.ToDate) || (dateEnd <= ct.ToDate && enddate >= ct.ToDate))
                                                                               select ct

您的帮助将不胜感激。提前THANX

1 个答案:

答案 0 :(得分:0)

查看以下代码:

var startdate= DateTime.ParseExact("2015-12-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
var enddate= DateTime.ParseExact("2015-12-07", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);

var result = (from ct in table
              where ct.IsWaiting && (ct.FromDate >= startdate && ct.ToDate <= enddate)
              select ct).ToList();