Linq2SQL Where Between

Can this be done in SQL in Linq to SQL?

Select field from table where date between '2010-01-01' and '2010-01-31'; 

I understand that I can:

 where (monthBeginDate < a.StopDateActual && a.StopDateActual < monthEndDate) 

But I was curious if I could do the first. I have a bad habit of screwing less and more than from similar statements.

+4
source share
2 answers

No, this is the way to do it. I believe that C # has no operator.

You can always cheat and write an extension method like me:

 public static bool BetweenDates (this DateTime checker, DateTime floor, DateTime ceiling) { return (checker <= ceiling) && (checker >= floor); } 

Then you can do it :-)

 .Where(s => s.StopDateActual.BetweenDates(monthBeginDate, monthEndDate)); 
+4
source

You can do the following:

 public bool Between(DateTime value, DateTime from, DateTime To) { return value > from && value < to; } where Between(a.StopDateActual, monthBeginDate, monthEndDate) 

However, you should notice that this works on IEnumerable , not on IQueryable , that is, the results will be pulled from the data source before it is applied. This can be a performance issue in case of a lot of data, so your classification is the best you can do ... just be careful s> and <!!!

+5
source

Source: https://habr.com/ru/post/1302412/


All Articles