0

可能重复:
linq to entity 无法识别方法

我正在尝试检查字符串是否以数字开头

var product09 = DataContext.Products.Where(x => Char.IsNumber(x.ProductName,0));

我也试过

var product09 = DataContext.Products.Where(x => Char.IsNumber(x.ProductName[0]));

我收到以下错误:

Boolean IsNumber(Char)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NotSupportedException: Boolean IsNumber(Char)

你能帮忙吗?

4

2 回答 2

2

根据您的评论,您正在使用 NHibernate。您应该能够使用以下CreateCriteria方法:

using (ISession session = sessionFactory.OpenSession())
{
    var result = session.CreateCriteria<Product>()
                        .Add(Restrictions.Like("ProductName", "[0-9]%"))
                        .List<Product>();
}

你也可以使用.Add(Restrictions.Like("ProductName", "[0-9]", MatchMode.Start))

另一种选择是使用 HQL:

var query = "from Product p where p.ProductName like '[0-9]%'";
var result = session.CreateQuery(query).List<Product>();

您可以将该SqlMethods.Like方法与 LINQ to SQL 一起使用:

var query = dc.Products.Where(x => SqlMethods.Like(x.ProductName, "[0-9]%"));
于 2012-04-30T22:31:23.033 回答
1

'怎么样:

char[] numbers = new[] { '0', '1', '2' , '3', '4', '5', '6', '7', '8', '9' };
DataContext.Products.Where(x => numbers.Contains(x.Substring(0, 1));
于 2012-04-30T22:29:22.400 回答