2

I need to refactor my app, changing magic sql strings with linq expressions. How convert following SQL-Query to LINQ

select et from tiporazmers where try_cast(et as float)>@param1` 

Note: I need to filter on database level, no filter collections in memory!

Any suggestions are helpful, thanks!

Michael Mairegger
  • 6,833
  • 28
  • 41
Lapenkov Vladimir
  • 3,066
  • 5
  • 26
  • 37
  • You mean to use it as LinqToSql or just pass it to the server? – kamil-mrzyglod Oct 09 '15 at 07:20
  • what's type of `et` in DB? – xZ6a33YaYEfmv Oct 09 '15 at 07:30
  • Possible duplicate of [Entity Framework/Linq EXpression converting from string to int](http://stackoverflow.com/questions/16694716/entity-framework-linq-expression-converting-from-string-to-int) – Backs Oct 09 '15 at 09:57
  • @Backs that link is out of date. The answers there do not involve building `Expression` tree with `Expression.Convert`. I believe that's was because at that time `Expression.Convert` had not been supported in EF. But now (at least since EF5) we can build Expression tree using `Expression.Convert`. BTW, I downvoted 2 answers there suggesting to use `Convert.ToInt32` (about a month ago) – Hopeless Oct 09 '15 at 10:01
  • You cannot do this without changing the database. You should change type of column to numeric type in table, or create view with cast and select from that view. – Kirill Bestemyanov Oct 09 '15 at 10:02

0 Answers0