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

How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
本文介绍了如何在 Linq To SQL 中知道字段是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要选择列值为数字的表的行,有什么帮助吗?

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

我有一个 varchar 列,我需要选择哪些是数字,哪些不是.

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 不能使用,因为它不能转换为 SQL.

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

推荐答案

我不知道 LinqToSQL 中是否有 int.TryParse() 的映射,但是您可以通过执行查询分两步完成,转换为列表,然后使用 LinqToObjects 从列表中选择.

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 ) );

你可能想看看 动态 LINQ,也是.这将允许您执行以下操作:

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

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

编辑 VS2008 代码示例中提供了动态 LINQ,链接来自 Scott Guthrie 的博客,我已在上面链接.

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

这篇关于如何在 Linq To SQL 中知道字段是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
How to store delegates in a List(如何将代表存储在列表中)
How delegates work (in the background)?(代表如何工作(在后台)?)
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)