c# - Applying a function within the select clause -


i have query (longish joins etc) , fields in select clause (all of of type decimal?), want return null when db has them stored 0. doing following work correctly:

var q =     o in ....     ....     select new     {         ...         spot = o.spot == 0 ? null : o.spot,         moneyness = o.strike / (o.spot == 0 ? null : o.spot),         volatility = o.rate == 0 ? null : o.rate     }; 

but prefer encapsulate check 0 in function. tried

private static decimal? nullifzero(decimal? obj) {     return  obj == 0 ? null : obj; } 

and then

var q =     o in ....     ....     select new     {         ...         spot = nullifzero(o.spot),         moneyness = o.strike / nullifzero(o.spot),         volatility = nullifzero(o.rate)     }; 

but error:

an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.linq.dll

additional information: not translate expression 'nullifzero(<>h__transparentidentifier0.o.spot)' sql , not treat local expression.

i not error if try

var q =     o in ....     ....     select new     {         ...         spot = nullifzero(o.spot),         moneyness = o.strike / (o.spot == 0 ? null : o.spot),         volatility = nullifzero(o.rate)     }; 

but can't see difference between nullifzero(o.spot) , (o.spot == 0 ? null : o.spot) is, when seems have effect when use divisor.

the first query fails because l2s can't translate function call nullifzero. second query works because l2s can evaluate local functions if part of last select in query. nice feature ef lacks right now.

solutions:

  1. inline function.
  2. try asexpandable predicate builder library.

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -