0

in this below code

string cluase = string.Empty;
string whereValue = string.Empty;

foreach (var i in whereCluaseItems)
{
    if (cluase != "")
    {
        cluase += " and " + i.Name;
    }
    else
    {
        cluase = i.Name;
    }
    if (whereValue != "")
    {
        whereValue += "," + i.Value;
    }
    else
    {
        whereValue = i.Value;
    }
}

var result = context.vCatalogItemsDetails
                    .Where(cluase, whereValue) 
// since where syntax has to be like this : where("@landuageID=@0,categoryid=@1", 1,1)
.OrderBy("itemID")
.Skip((pageN - 1) * 10).Take(10);

cluase is a string and it contains "languageId=@0, categoryid=@1"

whereValue is also a string and it contains "1,1"

i need to remove those quotation marks. how can i do it?

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Here's a great article on CodeProject: [Build Where Clause Dynamically in Linq](http://www.codeproject.com/Tips/582450/Build-Where-Clause-Dynamically-in-Linq). – John Woo Jul 29 '14 at 06:57

2 Answers2

1

For this kind of "dynamic" where clause, there is often no need to use expression based features. You just have to realize that multiple Where calls can be composed.

For example:

public static IEnumerable<Person> Filter(this IEnumerable<Person> source, 
    string firstname, string lastname)
{
    if (firstname != null)
    {
        source = from person in source
                 where person.Firstname.Contains(firstname)
                 select person;
    }

    if (lastname != null)
    {
        source = from person in source
                 where person.Lastname.Contains(lastname)
                 select person;
    }

    return source;
}

The same technique works with IQueryable<T> too.

Kris Vandermotten
  • 10,111
  • 38
  • 49
0

It is possible to do that with help of Dynamic LINQ library, it is then as simple as:

var result = context.vCatalogItemsDetails
                    .Where(cluase, whereValue) 
                    .Where("landuageID=1 AND categoryid=1")
                    .OrderBy("itemID")
                    .Skip((pageN - 1) * 10).Take(10);
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58