tags:

views:

23

answers:

1

I have an nvarchar SQL column which contains mostly numeric values. I'm trying to come up with an L2S expression that gets me the maximum numeric value while ignoring any non-numeric values.

The SQL that does the job is:

select top 1 value from identifier where and patindex('%[^0-9]%', value) = 0 order by CAST(value AS INT) desc

What LINQ expression can I use to achieve the same thing?

+4  A: 

You can do SQL Like queries using the SqlMethods in System.Data.Linq.SqlClient.

(from a in identifier
where !SqlMethods.Like(a.value, "%[^0-9]%")
select a).Max(x => Convert.ToInt64(x.value))

This Linq statement becomes this query according to LinqPad:

-- Region Parameters
DECLARE @p0 VarChar(8) = '%[^0-9]%'
-- EndRegion
SELECT MAX([t1].[value]) AS [value]
FROM (
    SELECT CONVERT(BigInt,[t0].[value]) AS [value], [t0].[value]
    FROM [Identifier] AS [t0]
    ) AS [t1]
WHERE NOT ([t1].[value] LIKE @p0)

LinqPad is a great way to play around with queries to see if you can get what you're looking for. I've found that just about the only SQL statements that don't have a good L2S equivalent are ones with the PIVOT keyword. Other than that, there's usually a way to get what you want.

If you want the whole record and not just the MAX() value, you could do the query this way:

(from a in Accounts
orderby (!SqlMethods.Like(a.AccountNumber, "%[^0-9]%") ?
                      Convert.ToInt64(a.AccountNumber) : 0)
descending
select a).Take(1)
mattmc3
The query fails though because the nested select is doing the conversion without excluding the non-numeric values.
sipwiz
Oops. I had the Like arguments switched. I've updated it and tested it against dummy data and the first query runs (SQL 2008).
mattmc3
I just worked that as well :). That'll teach me to look more closely at the SQL in the query analyzer.
sipwiz