一名前pression树不能包含动态操作 [英] An expression tree may not contain a dynamic operation

查看:144
本文介绍了一名前pression树不能包含动态操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,如果我尝试通过动态类型值转换成实体框架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);
 

解决方案

看一看<一href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx"相对=nofollow> DLINQ 它可以让你做的东西,如:

  VAR的查询=
    db.Customers。
    其中,(市= @ 0和Orders.Count&GT; = @ 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)

这篇关于一名前pression树不能包含动态操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆