Friday 15 March 2013

c# - Getting error while trying to use contains in Linq -



c# - Getting error while trying to use contains in Linq -

i array of strings client. in webapi split following

var splitid = string.join(",", ids);

then linq query utilize above variable

this linq query

var query = tablearepository.asqueryable() .where(a=> splitid.contains(a.id.tostring())) .select(a => new { id = a.id , name = a.name , fkid = tablebrepository.asqueryable().min(b => b.fkid=a.id) });

i exception:

linq entities not recognize method 'system.string tostring()' method, , method cannot translated store expression.","

this because of contains. how can prepare error?

instead of converting ids concatenated string, maintain them in list<t>/ienumerable<t> , query against like:

based on comment:

ids defined string[] ids

you should convert them ienumerable<int> or list<int> like:

list<int> integerids = ids.select(int.parse).tolist();

and in query:

var query = tablearepository.asqueryable() .where(a=> integerids.contains(a.id))

just create sure ids contains elements of same type table id

the reason getting exception due phone call tostring in linq query, provider trying convert under laying info source language (may sql) can't.

c# linq

No comments:

Post a Comment