views:

54

answers:

3

Hello, I have a column that holds VARCHARs and I have a flag that determines if the value is a string or integer. I would like perform a search on the integers but they are stored as VARCHAR. How do I do this?

I am trying to do this in a where clause.

var q = from x in context.table
        where x.Criteria > 0
        select new
        {
              x
        }
A: 

CAST(TheColumn AS INTEGER)

dan04
how would I do this in a where clause using linq
Luke101
A: 

sql

select isnull(Convert(int, varcharColumn),0)
from atable
where isInteger = 1

linq

var q = context.YourCustomView;
Holystream
how would I do this in a where clause using linq
Luke101
var q = context.YourTable.Where(i=> i.IsNumber).Select(i => Convert.ToInt32(i.VarcharColumn));
Holystream
A: 

You'll probably need to pull down the values from the server as strings and do the conversion in LINQ to Objects.

var q = context.table
    .Where(x => x.Criteria > 0)
    .Select(x => x.IntOrStringValue)
    .AsEnumerable()
    .Select(v => Convert.ToInt32(v));

replace x.Criteria > 0 with your logic that indicates the value is an integer. replace x.IntOrStringValue with your column containing the integers.

GWB
[Linq-to-SQL can translate `Convert.ToInt32()` just fine.](http://stackoverflow.com/questions/992189/)
Timwi
The question is tagged linq-to-entities, not linq-to-sql.
GWB