0

Possible Duplicate:
linq to entities doesn't recognize a method

I am trying to check if a string starts with a number

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

I also tried

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

I get the following error:

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)

Can you please help?

Community
  • 1
  • 1
TDaddy Hobz
  • 454
  • 4
  • 11

2 Answers2

2

Based on your comment you're using NHibernate. You should be able to use the CreateCriteria method:

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

You could also use .Add(Restrictions.Like("ProductName", "[0-9]", MatchMode.Start))

Another option is to use HQL:

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

You can use the SqlMethods.Like method with LINQ to SQL:

var query = dc.Products.Where(x => SqlMethods.Like(x.ProductName, "[0-9]%"));
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
1

'How about:

char[] numbers = new[] { '0', '1', '2' , '3', '4', '5', '6', '7', '8', '9' };
DataContext.Products.Where(x => numbers.Contains(x.Substring(0, 1));
John W
  • 1,472
  • 1
  • 14
  • 19