Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create LINQ Query from string?

Tags:

string

c#

var

linq

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?

like image 501
Sammm Avatar asked Feb 28 '11 08:02

Sammm


People also ask

How to convert string to LINQ query?

How can I convert this string to linq query? string linqstring="(from t1 in Context. Table1 join t2temp in Context. Table2 on t1.Id equals t2.Id into tempJoin from t2 in tempJoin.

Can you use LINQ on a string?

LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String.

How to write LINQ queries?

There are the following two ways to write LINQ queries using the Standard Query operators, in other words Select, From, Where, Orderby, Join, Groupby and many more. Using lambda expressions. Using SQL like query expressions.


2 Answers

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
    }
    
like image 104
Geoff Appleford Avatar answered Sep 28 '22 09:09

Geoff Appleford


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

like image 30
Stuart Avatar answered Sep 28 '22 08:09

Stuart