I’m currently developping an expression method (used in linq to entity queries) who has to give me
- a daycount for a given period (start date and end date)
- decrementing this daycount if specials days are in the period.
My idea was the following :
- Generate an enumerable with all the dates (and with Enumerable.Range)
- Make a .Where on this enumerable to remove the specials dates Like a MyEnumerable.Where(a => a != “20120101”)
- After that, return a MyEnumerable.Count()
I come with this code :
return (p) => Enumerable
.Range(1, 4)
.Where(a => a != 20120101)
.AsQueryable()
.Count()
I tried to cast as a list, as a queryable, both (like the example) and no way ! it doesn’t work ! I always get this error :
LINQ to Entities does not recognize the method ‘System.Collections.Generic.IEnumerable`1[System.Int32] Range(Int32, Int32)’ method, and this method cannot be translated into a store expression.
Have you got an idea about that ?
Using an enumerable is of course not mandatory, any working solutions is good ^^
Thank’s by advance !
To simplify my question, this don’t works and i want to.
(
from foo in User
select new
{
a = Enumerable.Range(1,4).Count()
}
).Take(1)
More informations 🙂
If i do this (externalize the enumerable in a variable) :
var foo = Enumerable.Range(1, 4);
return (p) => foo.Where(a => a != 20110101).Count();
This time it works, but i can’t define the range start and end with this method !
The code below generates a real DateTime range and will therefore correctly handle leap years, varying month ends, and so on. It also correctly handles daysToExclude being null or empty.