views:

711

answers:

10

Hi all,

I am writing an app for my company and am currently working on the search functionality. When a user searches for an item, I want to display the highest version (which is stored in a database).

The problem is, the version is stored as a string instead of int, and when I do an OrderBy(q=>q.Version) on the results, they are returned like

1
10
11
2
3
...

Obviously 2 comes before 10.

Is there a way for me to cast the version as an integer or is there a simple IComparer out there? I couldn't find anything substantial thus far.

I tried doing this:

var items = (from r in results
             select r).OrderBy(q => Int32.Parse(q.Version));

This compiles but doesn't work.

+2  A: 

Your problem is somewhere else, the following works:

new[] { "1", "10", "2", "3", "11" }
    .OrderBy(i => int.Parse(i))
    .ToList()
    .ForEach(Console.WriteLine);

If your problem is LINQ to SQL, then what is happening is CLR is trying to create SQL out of your LINQ and doesn't understand int.Parse. What you can do is first get the data from SQL then order it once all data is loaded:

var items = (from r in results
             select r)
            .ToList()
            .OrderBy(q => Int32.Parse(q.Version));

Should do it.

Yuriy Faktorovich
I get this error message:"Method 'Int32 Parse(System.String)' has no supported translation to SQL" when viewing the enumerated results.
Darcy
Casting to List (ToList) also works. Thanks yuriy.
Darcy
+1  A: 

Why are you sorting in a lambda? Why don't you just sort in the query?

var query = from r in items
            orderby int.Parse( r )
            select r;

Now that we know you are using LINQ to SQL, you might consider making a standard SQL call on this one by doing something like:

Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...

Or even

Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...
Order By Cast( TextWhichShouldBeIntCol As int )

That will bleed into your LINQ as an int (and if you use the second iteration, be ordered). That avoids having to go through the resultset twice in LINQ (once for querying, once for ordering).

Thomas
Its the same thing either way.
Darcy
No it does differ in performance - databases have good sorting processes and some can process building the result set in parallel so sorting in the database will usually be faster and very rarely slower.
Mark
Not only that, you are cycling through the list twice: once for the query and once for the lambda.
Thomas
+1  A: 

I made a test. I have the following code.

string[] versions = { "1", "2", "10", "12", "22", "30" };
foreach (var ver in versions.OrderBy(v => v))
{
     Console.WriteLine(ver);
}

As expected the result is 1, 10, 12, 2, 22, 30 Then lets change versions.OrderBy(v => v)) to versions.OrderBy(v => int.Parse(v))). And it works fine: 1, 2, 10, 12, 22, 30

I think your problem is that you have nondigit chars in your string like '.'. What kind of exception do you get?

ILya
A: 

try this:

var items = results.(Select(v => v).OrderBy(v => v.PadLeft(4));

that'll work in Linq2Sql

Mike Jacobs
+5  A: 

If you're unable to change your table definition (so the version is a numeric type), and your query really is as listed (your not using skip, or take, or otherwise reducing the number of results), the best you can do is call "ToList" on the unsorted results, which when you then apply an OrderBY lambda to it will take place in your code, rather than trying to do it at the SQL Server end (and which should now work).

Damien_The_Unbeliever
Ah this worked. Why wouldn't this work in the linq statement? Because in the linq it's trying to case on the server side?
Darcy
@Darcy: See here...http://www.atrevido.net/blog/2007/09/05/Calling+Custom+Methods+In+LINQtoSQL.aspx
Robert Harvey
Note that you could also use `AsEnumerable` instead of `ToList` which changes the query provider back to linq-to-objects, but keeps the deferred execution semantics of linq queries instead of forcing execution.
Greg Beech
@Greg - true. I just went with most direct (to pull the results locally). What the best option is depends on how the items variable is used.
Damien_The_Unbeliever
A: 

It sounds like you have a text value instead of a numeric value.

If you need to sort, you can try:

var items = (from r in results
             select r);
return items.OrderBy( v=> Int.Parse(v.Version) );
chris
A: 
var query = from r in items
            let n = int.Parse(r)
            orderby n
            select n;
Alxandr
A: 
var items = (from v in results
                    select v).ToList().OrderBy(x => int.Parse(x.Version));
Steven Williams
+1  A: 

Why are you sorting if you only need "the highest version"? It sounds like you could avoid some overhead if you used Max().

Also, you really should change the column type to integer.

Olivier Dagenais
A: 

Int32.Parse is not supported by the LinqToSql translator. Convert.ToInt32 is supported.

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

http://msdn.microsoft.com/en-us/library/bb882655.aspx

David B