简体   繁体   English

如何从字符串创建LINQ查询?

[英]How to create LINQ Query from string?

I am new at LINQ and really need a help with some coding. 我是LINQ的新手,需要一些编码方面的帮助。

At the moment, I have a string and a var variables. 目前,我有一个字符串和一个var变量。

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. 基本上,我想要做的是能够通过预先将其分配到字符串中来创建自定义/复杂的LINQ查询。 After done with composing, I assign the string into the var variable. 完成编写后,我将字符串分配给var变量。 However, this is obviously will not work. 但是,这显然是行不通的。 Therefore, can anyone assist me on this? 因此,任何人都可以帮助我吗?

You have a few options: 你有几个选择:

  • Use the the Dynamic Linq libraries to construct you queries on the fly. 使用Dynamic Linq库可以动态构建查询。 The best place to get started is by reading ScottGu's blog entry . 入门的最佳位置是阅读ScottGu的博客文章 However, I don't think these libraries support the contains method in your example. 但是,我不认为这些库支持您的示例中的contains方法。 Here is a blog post explaining how to add this support. 是一篇博客文章,解释了如何添加此支持。

  • Directly execute SQL statements. 直接执行SQL语句。 Check out the MSDN docs for Linq to Sql or Linq to Entities . 查看Linq to SqlLinq to Entities的MSDN文档。

     var _Products = myEntities.ExecuteStoreQuery<Product> (@"SELECT * FROM Products WHERE [Name] In ('Item1', 'Item2')"); 
  • Use Linq's composable behaviour . 使用Linq的可组合行为 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 } 

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. 你可以通过在一些c#中使用CodeDomProvider编译这个Linq来实现这一点 - 为.NET应用程序添加脚本功能 - 但这是一个非常重要的解决方案。 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! 如果你想看到更多关于如何做到这一点,那么看看LinqPad - http://www.linqpad.net - 作者邀请你使用反编译器看看它是如何工作的!

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 如果要求只是简单的条款,那么除了替代方案可能是使用Dynamic Linq - 请参阅Scott Gu的帖子和Microsoft的示例代码 - http://weblogs.asp.net/scottgu/archive/2008/01/07 /dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Maybe this can help you http://nlinq.codeplex.com/ 也许这可以帮助你http://nlinq.codeplex.com/

BR. BR。

Much of the reason you use LINQ in the first place is to get compiler-verified queries, that wil ldetect errors at compile time. 首先使用LINQ的主要原因是获得编译器验证的查询,这将在编译时检测错误。 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. 1)进行eSQL查询并在ObjectContext上运行它。 Using this, you can still use your entities like myEntities.Products, and return a list of products. 使用此功能,您仍然可以使用myEntities.Products等实体,并返回产品列表。

2) Using a normal SQL query, and use the ObjectContext to call that directly towards the underlying database. 2)使用普通的SQL查询,并使用ObjectContext直接调用底层数据库。

My guess is that you will have to use dynamic code execution. 我的猜测是你必须使用动态代码执行。 For further details, have a look at Ricks post on west-wind . 有关详细信息,请查看西风的 Ricks帖子。

you're thinking of it like a Dynamic SQL where you create the statement as string and parse it as a SQL statement. 您将其视为动态SQL,您可以将该语句创建为字符串并将其解析为SQL语句。

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. 如果你使用Lambda而不是传统的linq,那将会容易得多。 my 2 cents. 我的2美分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM