ToString() in LINQ expression
Posted: Mon 26 May 2014 06:05
Trying to create an expression to query against multiple combined fields that together create a unique ID. There's a prefix, id number, suffix and version number - the first 3 fields being strings and version number being a integer.
So querying against X1000T would query against the combined prefix X + id number 1000 + suffix T for version 0, using an expression that looks like this:
query = query.Where(n => (((n.prefix ?? "") + n.idnumber + (n.suffix ?? "") + (n.version == 0 ? n.ToString() : n.version.ToString()) ) == combined_id));
This generates an exception on the Tostring method:
LINQ to Entities does not recognize the method 'System.String ToString()' methodtranslated store expression
EF has a SqlFunctions.StringConvert method but this only works with SQL Server, need something similar for Postgres.
Is there an equivalent? Could one generate an extension to take care of this - and are there any examples illustrating something like this?
Any help is appreciated.
Regards,
So querying against X1000T would query against the combined prefix X + id number 1000 + suffix T for version 0, using an expression that looks like this:
query = query.Where(n => (((n.prefix ?? "") + n.idnumber + (n.suffix ?? "") + (n.version == 0 ? n.ToString() : n.version.ToString()) ) == combined_id));
This generates an exception on the Tostring method:
LINQ to Entities does not recognize the method 'System.String ToString()' methodtranslated store expression
EF has a SqlFunctions.StringConvert method but this only works with SQL Server, need something similar for Postgres.
Is there an equivalent? Could one generate an extension to take care of this - and are there any examples illustrating something like this?
Any help is appreciated.
Regards,