C# 如何在 Linq To SQL 中知道字段是否为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/558054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:06:08  来源:igfitidea点击:

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

c#sql-serverlinq-to-sql

提问by Jedi Master Spooky

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 ones that are not.

编辑:我有一个 varchar 列,我需要选择数字和非数字的列。

EDIT 2: Integer.TryParse cannot be use because it cannot be translate to SQL.

编辑 2:Integer.TryParse 不能使用,因为它不能转换为 SQL。

采纳答案by tvanfosson

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.

我不知道 LinqToSQL 中是否有 int.TryParse() 的映射,但您可以通过执行查询、转换为列表、然后使用 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:

您可能也想看看Dynamic LINQ。这将允许您执行以下操作:

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

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

编辑动态 LINQ 在 VS2008 代码示例中可用,链接到 Scott Guthrie 的博客,我在上面链接过。

回答by Ian P

Look into Int32.TryParse ... That may work for you.

查看 Int32.TryParse ......这可能对你有用。

Ian

伊恩

回答by Leandro López

(Edited to suit your needs)

(根据您的需要进行编辑)

So, given the fact that you cannot use Integer.TryParse, the following is not feasible:

因此,鉴于您不能使用Integer.TryParse,以下是不可行的:

int i;
var rowsWithInts = (From r In dc.YourRows Where Integer.TryParse(r.Column, i));
var rowsWithoutInts = (From r In dc.YourRows Where !Integer.TryParse(r.Column, i));

Is it possible that you create a stored procedure and link it to the DataContext via the Methods Pane? In that case you can do in your SQL the following:

是否可以创建一个存储过程并通过方法窗格将其链接到 DataContext?在这种情况下,您可以在 SQL 中执行以下操作:

-- Rows with integers
SELECT * FROM your_table WHERE ISNUMERIC(varchar_column) = 1
-- Rows without integers
SELECT * FROM your_table WHERE ISNUMERIC(varchar_column) = 0

Does this helps?

这有帮助吗?

回答by Mike Chaliy

You will not be able to do this with LINQ2SQL without loading all records on the client.

如果不加载客户端上的所有记录,您将无法使用 LINQ2SQL 执行此操作。

I can suggest to create view or table-valued function with aditional calculated column and then filter your results with this column. Or execute regular SQL with DataContext.ExecuteQuery.

我可以建议使用附加计算列创建视图或表值函数,然后使用此列过滤结果。或者使用 DataContext.ExecuteQuery 执行常规 SQL。

回答by Timothy Khouri

Open up your DBML (LINQ-to-SQL) file in an XML editor, go down to the end of the file and paste this just before the '</Database>' node:

在 XML 编辑器中打开您的 DBML (LINQ-to-SQL) 文件,转到文件末尾并将其粘贴到 '</Database>' 节点之前:

<Function Name="ISNUMERIC" IsComposable="true">
    <Parameter Name="Expression" Parameter="Expression" Type="System.String" DbType="NVarChar(4000)" />
    <Return Type="System.Boolean" DbType="BIT NOT NULL"/>
</Function>

Now, you can use the already-in-SQLfunction called "ISNUMERIC". Here's how:

现在,您可以使用名为“ISNUMERIC”的 SQL 中已有函数。就是这样:

var blah = myDataContext.Accounts.Where(account=>
    myDataContext.ISNUMERIC(account.ID) == true);

There you go :)

你去吧:)

You may also find these functions useful to copy:

您可能还会发现这些函数对复制很有用:

<Function Name="RAND" IsComposable="true">
  <Return Type="System.Double" DbType="Float NOT NULL" />
</Function>
<Function Name="NEWID" IsComposable="true">
  <Return Type="System.Guid" DbType="UniqueIdentifier NOT NULL" />
</Function>

回答by Timothy Khouri

<>.Where(x=>"0123456789".IndexOf(x.value.substring(0,1))>-1)

回答by Josh

SqlFunctions.IsNumeric

SqlFunctions.IsNumeric

http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.isnumeric.aspx

http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.isnumeric.aspx

For example:

例如:

    private int GetLastGoodNumber(string PartNum)
    {
        return (from row in Db.SerialNo
                where
                   row.PartNum == PartNum
                let nullable = System.Data.Objects.SqlClient.SqlFunctions.IsNumeric(row.SerialNumber)
                where nullable != null
                select nullable.Value).Max();
    }