c# - Total Sum with multiple where clause LINQ -


i have sql query works fine in sql server.

select sum(convert(float,[amount]))  [transaction]  [response_code] = '100' , merchant_id = '14 

while converting linq, resultant query , exception receiving. doing wrong?

linq =

float count = dbcontext.transactions.where(trx => trx.response_code.equals("100"))                             .where(trx=>trx.merchant_id.equals(mid))                             .sum(i => float.parse(i.amount)); 

error =

{system.invalidoperationexception: not translate expression 'table(transaction).where(trx => trx.response_code.equals("100")).where(trx => trx.merchant_id.equals(convert(invoke(value(system.func`1[system.nullable`1[system.int32]]))))).sum(i => parse(i.amount))' sql , not treat local expression.    @ system.data.linq.sqlclient.objectreadercompiler.generator.generate(sqlnode node, localbuilder locinstance)    @ system.data.linq.sqlclient.objectreadercompiler.generator.generatebody(ilgenerator generator, sqlexpression expression)    @ system.data.linq.sqlclient.objectreadercompiler.compiledynamicmethod(generator gen, sqlexpression expression, type elementtype)    @ system.data.linq.sqlclient.objectreadercompiler.compile(sqlexpression expression, type elementtype)    @ system.data.linq.sqlclient.sqlprovider.getreaderfactory(sqlnode node, type elemtype)    @ system.data.linq.sqlclient.sqlprovider.system.data.linq.provider.iprovider.execute(expression query)    @ system.data.linq.dataquery`1.system.linq.iqueryprovider.execute[s](expression expression)    @ system.linq.queryable.sum[tsource](iqueryable`1 source, expression`1 selector)    @ wcf_soap_rest_service.service.totalacceptedtransactioncount(nullable`1 mid) in c:\users\monet\documents\visual studio 2012\projects\wcf_soap_rest_service\service.svc.cs:line 845} 

the problem linq entities doesn't recognize method float.parse() , can not translate valid store statement.

you have enumerate first result methods select(), asenumerable(), tolist()...

here have methods linq entities can translate valid sql statement:

clr method canonical function mapping

this should work:

dbcontext.transactions.where(trx => trx.response_code.equals("100"))     .where(trx=>trx.merchant_id.equals(mid))     .tolist() // or asenumerable(), or select()...     .sum(i => float.parse(i.amount)); 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -