Convert string to long type and use in a linq query within asp.net MVC


Convert string to long type and use in a linq query within asp.net MVC



Is it possible within Linq in C#, to convert a string field in a database, to a long type - and use it in the query?



Here, tme is a unix time (long) - but the field in the database, targetdate - is a string.



I've tried:


var qbt = db.Calls
.Where(x => x.team == id && long.Parse(x.targetdate) <= tme);



However I get the message: LINQ to Entities does not recognize the method 'Int64 Parse(System.String)' method, and this method cannot be translated into a store expression.


LINQ to Entities does not recognize the method 'Int64 Parse(System.String)' method, and this method cannot be translated into a store expression.



I know you can convert before the linq query, but is there any way of using it WITHIN the linq query?



Thanks for any help,



Mark





"but the field in the database, targetdate - is a string" - there's your first problem....
– Mitch Wheat
May 29 '13 at 10:26





4 Answers
4



try


var qbt = db.Calls.ToList()
.Where(x => x.team == id && long.Parse(x.targetdate) <= tme);



if you have many records you can limit them by team first and then call ToList like below


var qbt = db.Calls.Where(x => x.team == id).ToList()
.Where(i=>long.Parse(i.targetdate) <= tme);



Or You can use AsEnumerable


AsEnumerable


var qbt = db.Calls.AsEnumerable()
.Where(x => x.team == id && long.Parse(x.targetdate) <= tme);





this will bring whole table to memory.
– emre nevayeshirazi
May 29 '13 at 10:33





There is no need to use ToList(), instead use AsEnumerable() so that the items are still processed one-by-one without pulling everything into a list.
– Knaģis
May 29 '13 at 10:44


ToList()


AsEnumerable()





AsEnumerable() will achieve the same thing but with less memory consumption and probably with better performance.
– Knaģis
May 29 '13 at 10:57


AsEnumerable()





@Knaģis correct, Thanks for pointing out
– Damith
May 29 '13 at 11:01



This is to do with the way the Linq is translated into the backing query language, it might be easier to do a string comparison in this case, using tme.ToString(). If you pull the full collection down first, you could query like this but that means what it says: pulling down the full unfiltered (or at least less filtered) set.


tme.ToString()



You have to either change the database table to not store a string (you could create a computed column that converts it to a long or create a view if you cannot modify the existing table) or compare the value as string. The reason is that Entity Framework LINQ provider does not understand long.Parse and there is no method in SqlFunctions class for this purpose.


long.Parse


SqlFunctions


var stringTme = tme.ToString(CultureInfo.InvariantCulture);

var qbt = db.Calls
.Where(x => x.team == id && ((x.targetdate.Length < stringTme.Length)
|| (x.targetdate.Length == stringTme.Length && x.targetdate <= stringTme)));



You have to either change the database table to not store a string or compare the value as string. The reason is that Entity Framework LINQ provider does not understand long.Parse and there is no method in SqlFunctions class for this purpose.please use long.Parse()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux