views:

51

answers:

1

Help me with this algorithm please.

 var companies = companyrepository.GetAll().OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);
string where = "";

if (op == "eq")
  where = field + "=" + data;
else if (op == "cn")
  where = field + " LIKE '%"+data+"%'"; ///here lies my problem

companies = companies.Where(where);

its for adding dynamic where clauses to a linq query... op, field, and data are all strings that come with ajax from a jquery grid.

The problem is that it gives me an error when it tries to do the Like operator... it works just fine with the equals operator.

A: 

Based on this page, the following should work for you:

if (op == "eq")
  where = field + "=" + data;
else if (op == "cn")
  where = field + ".Contains(\"" + data + "\")";

I ran some tests on my own data, and it appears to work fine.

Ryan Versaw