Search code examples
c#linqlinq-to-sqllinq-to-entities

How to get the List of products name whose name starts with numbers using LINQ query.?


I have a table from where i want to retrieve the list of products whose name starts with 0 to 9. Code snippet.

List<string> searchItems = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };

.....Where(x=> searchItems.Any(y=> y.StartsWith(x.ProductName))).ToList();

I am not getting the list of products, getting 0 records, Anyone plz suggest me a better and correct query. Thanks in advance.


Solution

  • Try x.ProductName.StartsWith(y) rather than y.StartsWith(x.ProductName).

    The way you're doing it now you're asking "1".StartsWith("Your Product name").