Dynamic linQ on string array

arrays dynamic-linq

Question

I have a string array like this.

string[] ColumnArray = new string[] { First story, second data , third way };

Following is the linQ query on this array.

string query = (from x in ColumnArray 
                           where x.Contains("Story")
                            select x).First();

But sometimes the query will be like this.

string query = (from x in ColumnArray 
                         where ( x.Contains("Story") || x.Contains("View"))
                         select x).First();

That condition should add dynamically. SO how the dynamic LinQ can helps here.

I have written something like this.

string dynamiccondition= // some condition.

 var query = (from x in ColumnArray.AsEnumerable().AsQueryable().Where(dynamiccondition).Select(x));

// but this is not working.

Any suggestion?

1
0
1/29/2014 1:51:43 PM

Accepted Answer

In DynamicLINQ you can use logical operation like AND(&&) and OR(||), so try something like this

string dynamiccondition="it.Contains(\"Story\") OR it.Contains(\"View\")"

var query = ColumnArray.AsQueryable()
                       .Where(dynamiccondition);
1
1/29/2014 2:21:52 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow