Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

<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-SQL function called "ISNUMERIC". Here's how:

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...