表中给定特定记录的日期范围(例如特定字段“名称”)
如果有人试图在之前的日期范围内插入该名称,则不应允许这样做。
我在这里尝试过一些代码看看这个......
if (dataContext.TableAs.Where(
x => x.EndDate > StartDate &&
x.Name == Name).Count() == 0)
{
//insert record
}
但并不总是成功。
谁能建议我在这里缺少什么?
我已经在 SQL 中尝试过以下查询,如何在 LINQ 中使用上面的代码
SELECT COUNT(*) FROM TableA WHERE ('2012-04-02' between StartDate and EndDate or '2012-08-28'
between StartDate and EndDate or StartDate between '2012-04-02' and '2012-08-28' or EndDatebetween '2012-04-02' and '2012-08-28' ) and Name='Test'
尝试这个;
if (dataContext.TableAs
.Where(x => x.Name == Name)
.Max(x => x.EndDate) < StartDate)
编辑-对于问题的第二部分
DateTime Date1 = new DateTime("2012-04-02");
DateTime Date2 = new DateTime("2012-08-28");
var query = (dataContext.TableAs
.Where(x => x.Name == "Test")
.Where(x => (x.StartDate >= Date1 && Date1 <= x.EndDate)
|| (x.StartDate >= Date2 && Date2 <= x.EndDate)
|| (Date1 >= x.StartDate && x.StartDate <= Date2)
|| (Date1 >= x.EndDate && x.EndDate <= Date2))).Count();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)