Muntaqim Umatiya
Muntaqim Umatiya

Reputation: 41

Linq dynamic where clause query to find rows which contains 1st charater is numeric

I have following code to find where clause to search rows which contains numeric on first character of column if you have solutions please reply. below code result but only single row is returning

var strs = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
query = query.Where("@0.Contains(Name.Substring(0,1))", strs);
query = query.Where("Name.Substring(0,1).Contains(@0)", strs);
query = query.Where("Name.Contains(@0)", strs);
query.Where("Name.StartsWith(\"[0-9]\")");

Upvotes: 1

Views: 280

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205729

You can use the following:

query = query.Where("@0.Contains(outerIt.Name.Substring(0, 1))", (object)strs);

There are two Dynamic LINQ specifics.

First, the last argument of most of the dynamic methods is declared as params object[] values, so in order to pass the string array as single variable, you need to cast it to object, otherwise since string[] is castable to object[], it will be passed as variables and the @0 will basically refer to strs[0].

Second, once you pass the strs correctly, for some reason you have to use outerIt inside the Contains method to access the entity property.

P.S. Another way is to get rid of the strs array and use the following criteria:

query = query.Where("Name.Substring(0, 1) >= \"0\" and Name.Substring(0, 1) <= \"9\"");

Upvotes: 1

Related Questions