不介于linq和sql之间

Chl*_*bta 2 c# sql sql-server linq-to-sql

这是我的查询:

var model = (from p in entity.vehicule
             join y in entity.indisponible on p.Matv equals y.idv
             where p.agence.idgov == idv && (!(dd1 >= y.Dd && dd1 <= y.Df) || !(df1 >= y.Dd && df1 <= y.Df))
             select p).ToList();
Run Code Online (Sandbox Code Playgroud)

我尝试过很多方法来编写这个部分:

(!(dd1 >= y.Dd && dd1 <= y.Df) || !(df1 >= y.Dd && df1 <= y.Df))
Run Code Online (Sandbox Code Playgroud)

这样(这是在sql中看它的方式):

(dd1 Not Between Date(y.dd) And Date(y.dF)) OR (df1 Not Between Date(y.dd) And Date(y.df))
Run Code Online (Sandbox Code Playgroud)

dd1是日期(From),Df1是date(to).

我想我在这里遗漏了一些东西:(

Ric*_*ard 5

编辑2:经过多次评论后,希望这是你所追求的结果

public IList<Car> GetCarsAvailable(DateTime fromDate, DateTime toDate)
{
    var result = from c in dataContext.Cars
                 where !c.Bookings.Any(b => (fromDate >= b.From && fromDate <= b.To) || (toDate >= b.From && toDate <= b.To))
                 select c;

    return result.ToList();
}
Run Code Online (Sandbox Code Playgroud)

编辑1

如果我们稍微改变它而不是检查生日,我们将检查favourite days.不要让我们假设一个人可以有多个最喜欢的日子,并且我们想要选择没有最喜欢的一天的所有人,即2天内.让我们进一步写出我们的假设:

  • Richard最喜欢的日子是,5 May 201210 September 2012
  • Amy最喜欢的日子是,8 August 201212 December 2012
  • Matthews'最喜欢的日子是, 30 October 2012

让我们说,我们要找到大家谁没有之间的最喜欢的一天1 May 20121 September 2012; 我们的结果输出应该只是Matthew,我们可以写:

public IList<Person> GetPeopleWhoDontHaveAnyFavouriteDate(DateTime fromDate, DateTime toDate)
{
    var result = from p in dataContext.People
                 where !p.FavouriteDates.Any(f => f.Date >= fromDate && f.Date <= toDate)
                 select p;

    return result.ToList();
}
Run Code Online (Sandbox Code Playgroud)

上述陈述所说的是,我们想要选择所有人,但只有none他们最喜欢的日期在两个日期之间.

或者我们可以说,让我们选择一个人,如果他们确实有一个范围之外的日期.因此,假如我们希望从检查1 May 20121 November 2012,所以我们的结果集现在RichardAmy,这可能像这样来实现:

public IList<Person> GetPeopleWhoDontHaveFavouriteDate(DateTime fromDate, DateTime toDate)
{
    var result = from p in dataContext.People
                 where p.FavouriteDates.Any(f => f.Date < fromDate || f.Date > toDate)
                 select p;

    return result.ToList();
}
Run Code Online (Sandbox Code Playgroud)

原版的

我发现阅读你的缩写变量很棘手,所以我希望你不介意,但我想我会写一个快速演示如何做两个日期之间的"不在之间".

我认为你的东西是正确的.以下是您可以采用的几种方法.以下方法执行相同的操作,但是一个检查反向.

public IList<Person> GetPeopleNotBornFromTo(DateTime fromDate, DateTime toDate)
{
    var result = from p in dataContext.People
                 where p.DateOfBirth < fromDate || p.DateOfBirth > toDate
                 select p;

    return result.ToList();
}

public IList<Person> GetPeopleNotBornFromTo2(DateTime fromDate, DateTime toDate)
{
    var result = from p in dataContext.People
                 where !(p.DateOfBirth >= fromDate && p.DateOfBirth <= toDate)
                 select p;

    return result.ToList();
}
Run Code Online (Sandbox Code Playgroud)