-5

I am trying to write a Linq query that uses like %[0-9]% from sql-server.

This is my start:

string[] partNumbers = new string[] 
{ 
    "India", "US","UK", "Australia","Germany", "1", "7", "9" , "50", "A1"
};

var result = partNumbers.OrderBy(x => x).ToList();

How to write a linq query in c# which contains like "%[0-9]%"

This is the expected output:

1
7
9
50
A1

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • 3
    First write a method that performs the operation on a single value in your sequence. Then it should be a very simple matter of looking through the given linq operators to see how to apply that operation on the entire sequence to get all of the values where that given condition is met. – Servy Dec 10 '13 at 17:41

2 Answers2

2

You could use Regular expression

var result = partNumbers.Where(x => Regex.Match(x, "\\d+").Success);
Magnus
  • 45,362
  • 8
  • 80
  • 118
1

This may helps:

string[] partNumbers = new string[] 
{ 
    "India", "US","UK", "Australia","Germany", "1", "7", "9" , "50", "A1"
};

char[] numbers = {'0','1','2','3','4','5','6','7','8','9'};

var result = partNumbers.Where(i=>i.Intersect(numbers).Any()).ToList();

Result:

1
7
9
50
A1

EDIT: if you are looking for LIKE operator in SQL you can use the SqlMethods.Like method.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116