将linq查询创建为字符串

You*_*dir 4 c# linq string

我有一个包含linq查询的字符串,我有一个动态where子句也作为包含许多动态条件的字符串这里是我的where子句

string strWhereString = "where a.id==1 && a.name==\"something\"";
Run Code Online (Sandbox Code Playgroud)

这是我的linq查询字符串:

var query = "from a in context.tblName "+strWhereString;
Run Code Online (Sandbox Code Playgroud)

问题是如何运行此查询并从表中获取结果?有没有办法做到这一点或Linq不支持这个?

Mar*_*ark 7

您正在寻找的是像System.Linq.Dynamic

这将为您提供翻译查询的可能性,如:

var query = from p in northwind.Products
                where p.CategoryID == 3 && p.UnitPrice > 3
                orderby p.SupplierID
                select p;
Run Code Online (Sandbox Code Playgroud)

成:

var query = northwind.Products
                         .Where("CategoryID = 3 AND UnitPrice > 3")
                         .OrderBy("SupplierID");
Run Code Online (Sandbox Code Playgroud)

这里也是一个很好的起点,它有一个很好的博客文章和一些下载的例子.

动态LINQ(第1部分:使用LINQ动态查询库)