0

I have the following line of code:

group p by new DateTime((p.DateTime.Value.Ticks / interval.Ticks) * interval.Ticks).TimeOfDay

This works great in LINQ-to-Objects but it will not work in LINQ-to-Entities. I have two quetsions:

  • Why does it work? I don't understand how dividing the time by an interval and then multiplying it by that same interval magically converts my 12:37 to 12:30. My basic experience with math says that (A/B)*B = A. (Cleared up by @spencer in comments. Much thanks!)

  • How can I reformat this line to work in LINQ-to-Entities?

4

1 に答える 1

0

The DateTime methods you are using can't be translated by l2e. Consider using the EntityFunctions class for supported time operations.

See here for more details.

For instance, say your interval were 60 seconds, I'm guessing you might do something like:

DateTime? start=new DateTime(2000,1,1); //start of first interval
...group p by 
    EntityFunctions
        .AddSeconds(
            start,
            (EntityFunctions
                .DiffSeconds(p.DateTime,start)/60)*60
         )
于 2012-06-27T23:54:29.063 に答える