How to dynamically add OR operator to WHERE clause in LINQ

后端 未结 4 800
花落未央
花落未央 2020-11-30 07:36

I have a variable size array of strings, and I am trying to programatically loop through the array and match all the rows in a table where the column \"Tags\" contains at le

4条回答
  •  感情败类
    2020-11-30 07:56

    There is another, somewhat easier method that will accomplish this. ScottGu's blog details a dynamic linq library that I've found very helpful in the past. Essentially, it generates the query from a string you pass in. Here's a sample of the code you'd write:

    Dim Northwind As New NorthwindDataContext
    
    Dim query = Northwind.Products _
                         .Where("CategoryID=2 AND UnitPrice>3") _
                         .OrderBy("SupplierId")
    
    Gridview1.DataSource = query
    Gridview1.DataBind()
    

    More info can be found at scottgu's blog here.

提交回复
热议问题