it is trying to run Contains on a NULL - and where does that NULL come from? A tricky little property in yourControlParameter that defaults to true, and you have to manually change it to false:
public class FilterContract
{
public enum FilterOperator
{
Greaterthan,
Lessthan,
Equalto,
In,
Rangenum,
Rangedate,
Startswith,
Like
}
public string Colname { get; set; }
public List<object> Colvalue { get; set; }
public FilterOperator Filteroperator { get; set; }
}
and here is the place where i am using the code in Switch case.
-------------------------------
First I'd like to say that List<object>'s defination isn't so good……List<T> for you to define a generic List of specific type T,and we don't use object。Maybe you can use ArrayList instead。
And now your main question is that you should add a single quote——
@black
Member
60 Points
62 Posts
Dynamic linq with where clause --- contains
Feb 03, 2012 08:37 PM|LINK
I have a dynamic linq query with contains condition in it. But it gives me the following error: -
No applicable method 'Contains' exists in type 'Int32'
fc.Colvalue[0] is 'int' datatype.
coln = fc.Colname; colv = fc.Colvalue[0].ToString(); value = coln + ".Contains" + "(" + colv + ")"; inputdata = inputdata.AsQueryable().Where(value).ToList();rajsedhain
Contributor
4181 Points
1041 Posts
Re: Dynamic linq with where clause --- contains
Feb 03, 2012 08:46 PM|LINK
it is trying to run Contains on a NULL - and where does that NULL come from? A tricky little property in yourControlParameter that defaults to true, and you have to manually change it to false:
see here more:
http://naspinski.net/post/No-applicable-method-Contains-exists-in-type-String.aspx
Raj Sedhain
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamic linq with where clause --- contains
Feb 05, 2012 12:49 AM|LINK
Hello @black:)
Would you mind showing us the full codes?
@black
Member
60 Points
62 Posts
Re: Dynamic linq with where clause --- contains
Feb 06, 2012 01:18 PM|LINK
Its not a control its a list which holds values
@black
Member
60 Points
62 Posts
Re: Dynamic linq with where clause --- contains
Feb 06, 2012 01:20 PM|LINK
@Decker :-
Here it is .......
public class FilterContract { public enum FilterOperator { Greaterthan, Lessthan, Equalto, In, Rangenum, Rangedate, Startswith, Like } public string Colname { get; set; } public List<object> Colvalue { get; set; } public FilterOperator Filteroperator { get; set; } } and here is the place where i am using the code in Switch case. -------------------------------public static GridServiceOutPut GetFilteredDataTemp<T>(GridInput gridinput, List<T> inputdata)
{
foreach (FilterContract fc in gridinput.FilContract)
{
var coln = string.Empty;
var colv = string.Empty;
string value = string.Empty;
switch (fc.Filteroperator)
{
case FilterContract.FilterOperator.Equalto:
coln = fc.Colname;
colv = fc.Colvalue[0].ToString();
value = coln +
"=" + colv;
inputdata = inputdata.AsQueryable().Where(value).ToList();
break;
case FilterContract.FilterOperator.Like:
coln = fc.Colname;
colv = fc.Colvalue[0].ToString();
value = coln +
".Contains" + "(" + colv + ")";
inputdata = inputdata.AsQueryable().Where(value).ToList();
break;
}
}
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamic linq with where clause --- contains
Feb 07, 2012 12:04 AM|LINK
First I'd like to say that List<object>'s defination isn't so good……List<T> for you to define a generic List of specific type T,and we don't use object。Maybe you can use ArrayList instead。
And now your main question is that you should add a single quote——
".Contains" + "('" + colv + "')";
Or just use parameter+value——
http://stackoverflow.com/questions/2455659/how-to-use-contains-or-like-in-a-dynamic-linq-query
@black
Member
60 Points
62 Posts
Re: Dynamic linq with where clause --- contains
Feb 07, 2012 02:24 PM|LINK
Used single paranthesis as suggested. It gives error as "Character literal must contain exactly one character"
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamic linq with where clause --- contains
Feb 08, 2012 12:28 AM|LINK
What did you write?It should be——