一名前pression树不能包含动态操作一名、操作、动态、pression

2023-09-04 01:17:33 作者:Review(旧爱)

我,如果我尝试通过动态类型值转换成实体框架LINQ查询得到这个错误。

 动态SNAME =SURAJ; //即使对象,VAR
APPUSER APPUSER = Ctx.AppUsers.First(U => u.Name == SNAME);
 

如果我尝试先储存的值的字符串,并使用它,我得到 对象引用错误。

  VAR名称=SURAJ;
字符串的sname =新的字符串(((字符串)名).ToCharArray());

APPUSER APPUSER = Ctx.AppUsers.First(U => u.Name == SNAME);
 

解决方案

看一看 DLINQ 它可以让你做的东西,如:

  VAR的查询=
    db.Customers。
    其中,(市= @ 0和Orders.Count> = @ 1,伦敦,10)。
    排序依据(公司名称)。
    选择(新(公司名称作为名称,电话));
 
格斗中的 芭蕾 勇士击剑俱乐部

需要注意的是EX pressions查询字符串是本来可以动态构造在运行时。

该库有一些非常非常好的东西,包括隐式转换为EX pression树,你将能够顺利地集成到现有的前pression树。

( DLINQ是pretty的惊人的,当你想关闭它是如何书面方式在2006年左右,仍然是正确的C#技术进步的前面;的下载包含在这里\ LinqSamples \ DynamicQuery目录)

I get this error if I try to pass dynamic type value into entity framework linq query.

dynamic sname = "suraj";    // even object, var
AppUser appUser = Ctx.AppUsers.First(u => u.Name == sname);

If i try to store the value first in string and use it, I get "object reference error".

var name = "suraj";
string sname = new string(((string)name).ToCharArray());

AppUser appUser = Ctx.AppUsers.First(u => u.Name == sname);

解决方案

Have a look at DLINQ which allows you to do stuff like:

var query =
    db.Customers.
    Where("City = @0 and Orders.Count >= @1", "London", 10).
    OrderBy("CompanyName").
    Select("new(CompanyName as Name, Phone)");

Note that expressions in the query are strings that could have been dynamically constructed at run-time.

The library has some very very nice goodies, including implicit conversion to Expression Trees, that you will be able to smoothly integrate into your existing expression tree.

(DLINQ is pretty amazing when you think off how it was writting around 2006 and still is right on the front of C# technological advancements; Download included in the \LinqSamples\DynamicQuery directory here)