How to know if a field is numeric in Linq To SQL

前端 未结 7 1107
日久生厌
日久生厌 2020-12-15 11:18

I need to select the rows of a table where a column value is numeric, any Help?

EDIT: I have a varchar column and I need to select the ones that are numbers and the

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 11:41

    I don't know if there is a mapping for int.TryParse() in LinqToSQL, but you could probably do it in two steps by performing the query, casting to a List, then selecting out of the list with LinqToObjects.

    int i;
    var query = context.Table.ToList();
    var intQuery = query.Where( t => int.TryParse( t.Column, out i ) );
    

    You might want to look at Dynamic LINQ, too. That would allow you to do something like:

    var query = context.Table.Where( "IsNumeric(Column)" );
    

    EDIT Dynamic LINQ is available in the VS2008 Code Samples, linked to from Scott Guthrie's blog, which I've linked above.

提交回复
热议问题