I am having a problem giving a LINQ predicate the knowledge it needs to sort either alphabetically or numerically.
I am working with a multi-value pivoted set of data with has a sortable column. The column is always stored as varchar in the DB, however, it has lookup knowledge of what that data type actually is. To simplify, in addition to strings the column can contain integers.
I have written a general-purpose (and working) sorting predicate as follows.
Func<MyObject, string> sortClause = p => p.MyObjectProperties.Find(s => s.Name == theSortProperty).Value;
The problem is that if my sortable column contains numerical values (stored as varchar in the DB), I get
13
131 <-- 131 between 13 and 14!!!
14
15
Of course, alphanumerically this is correct.
As I said, I know programmatically the type of data (string or integer) for theSortProperty. I want to be able to instruct the predicate to sort using alphabetical means OR numerical means. I see no easy way to accomplish this, since my LINQ to SQL mapping declares that column as [Column(Storage="_Value", DbType="NVarChar(MAX)", UpdateCheck=UpdateCheck.Never)]
Yet I am sure someone must have seen this before and can offer a suggestion. I should mention that NHibernate or other ORMs are out of the question for me.....Thanks