c# - Odd LINQ/EF Behavior when Grouping by DateTime -


i have bit of code i've been using months without problem , today datetime overflow error.

the original code looks this

var payments = (from p in context.payments                 p.paymenttype == "direct"                    && p.exportfile == null                 group p new { p.customer, p.debtorid, p.receiptnumber, p.paymentdate } g                 select new                        {                           g.key.customer,                           g.key.debtorid,                           amount = g.sum(b => b.amount) * -1,                           receiptnumber = g.key.receiptnumber ?? default(long),                           paymentdate = g.key.paymentdate != null ?  (datetime)g.key.paymentdate : datetime.minvalue                        }).tolist(); 

whats odd is, in particular instance, code selecting 1 row of data looks this:

id  customer    debtorid    amount  receiptnumber   exportfile  paymenttype paymentdate 99  183245      672         5       419             null        direct      2015-06-10 00:00:00.000 

pretty straight forward. nothing wrong date.

stranger still, if make select without grouping, works fine.

var payments = (from p in context.payments         p.paymenttype == "direct"         && p.exportfile == null         select new         {             p.customer,             p.debtorid,             p.amount,             p.receiptnumber,             paymentdate = (datetime)p.paymentdate         }).tolist(); 

so, gives? said, code works fine leads me believe there's wrong data. data looks fine well. may relevant i'm retrieving data sql server ce database. maybe not.

any suggestions or ideas?

after tip oliver, found this: an overflow occurred while converting datetime using ef4

turns out .net datetime.minvalue outside of range of possible values sql ce datetime. logically, wouldn't expect problem because 1) i'm not trying save value database 2) value i've pulled database isn't null anyway, minvalue shouldn't come play, , 3) bit of code has worked until recently. oh, well, what's logic got it?

thanks, guys.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -