14

I am new at LINQ and really need a help with some coding.

At the moment, I have a string and a var variables.

string temp = "from product in myEntities.Products where product.Name.Contains(_Name) select product";
var _Products = temp;
LvProducts.DataSource = _Products;
LvProducts.DataBind();

Basically, what I want to do is to be able to create a custom/complicated LINQ query by assigning it into a string beforehand. After done with composing, I assign the string into the var variable. However, this is obviously will not work. Therefore, can anyone assist me on this?

Majid
  • 13,853
  • 15
  • 77
  • 113
Sammm
  • 501
  • 4
  • 9
  • 25

6 Answers6

22

You have a few options:

  • Use the the Dynamic Linq libraries to construct you queries on the fly. The best place to get started is by reading ScottGu's blog entry. However, I don't think these libraries support the contains method in your example. Here is a blog post explaining how to add this support.

  • Directly execute SQL statements. Check out the MSDN docs for Linq to Sql or Linq to Entities.

    var _Products = myEntities.ExecuteStoreQuery<Product>
    (@"SELECT * FROM Products WHERE [Name] In ('Item1', 'Item2')");
    
  • Use Linq's composable behaviour. This might not be the most elegant solution but it works really well if you do not have too many options. You can just construct your query in multiple parts.

    var _Products = from product in myEntities.Products
                    select product
    
    _Products = from product in _Products 
                where product.Name.Contains(_Name)
                select product
    
    if FilterByPrice {
        _Products = from product in _Products 
                    where product.Price > 100 
                    select product
    }
    
Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85
8

You can do this by compiling this Linq within some c# using the CodeDomProvider - Adding scripting functionality to .NET applications - but this is quite heavyweight as a solution. If you want to see more about how to do this, then take a look at LinqPad - http://www.linqpad.net - the author invites you to use the decompiler to see how it works!

If the requirement is just down to simple where clauses than an alternative might be to use Dynamic Linq - see Scott Gu's posts and the sample code from Microsoft - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
3

Maybe this can help you http://nlinq.codeplex.com/

BR.

eka808
  • 2,257
  • 3
  • 29
  • 41
0

Much of the reason you use LINQ in the first place is to get compiler-verified queries, that wil ldetect errors at compile time. This will defeat that purpose since the string will be parsed at runtime.

For your needs you have two options:

1) Making a eSQL query and running it on the ObjectContext. Using this, you can still use your entities like myEntities.Products, and return a list of products.

2) Using a normal SQL query, and use the ObjectContext to call that directly towards the underlying database.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
0

My guess is that you will have to use dynamic code execution. For further details, have a look at Ricks post on west-wind.

fjdumont
  • 1,517
  • 1
  • 9
  • 22
-3

you're thinking of it like a Dynamic SQL where you create the statement as string and parse it as a SQL statement.

Since you're already in the code, why not make the statements right in there. It would be a lot easier if you use Lambda instead of the traditional linq. my 2 cents.

Martin Ongtangco
  • 22,657
  • 16
  • 58
  • 84