表达式树不能包含动态操作

发布于 2024-10-31 08:24:49 字数 391 浏览 0 评论 0 原文

如果我尝试将动态类型值传递到实体框架 linq 查询中,则会收到此错误。

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

如果我尝试首先将值存储在字符串中并使用它,我得到 “对象引用错误”。

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

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-11-07 08:24:49

看看 DLINQ 允许您执行以下操作:

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

请注意,查询中的表达式是可以在运行时动态构造的字符串。

该库有一些非常非常好的功能,包括到表达式树的隐式转换,您将能够顺利地将其集成到现有的表达式树中。

当您回想起 2006 年左右编写的 DLINQ 时,它是相当令人惊奇的,并且仍然处于 C# 技术进步的前沿下载包含在此处的 \LinqSamples\DynamicQuery 目录

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)

放血 2024-11-07 08:24:49

与@Suraj的答案类似,因为dynamic在委托中显然没问题(Func )而不是表达式,那么您可以将委托转换为表达式 :

dynamic config = JsonConvert.DeserializeObject(configJsonString);
var typeName = config.type.ToString(); // clause expects a string, just separating it out for readability

// the guts of your clause --
// we'll turn this into an expression with o => wrapper(o)
Func<TEntity, bool> wrapper = (n => n.Name == typeName);

// wrap to expression and use as regular clause
var expectedType = repository.Where(o => wrapper(o)).FirstOrDefault();

Similar to @Suraj's answer, since dynamic is apparently okay in a delegate (Func) but not an Expression, then you can convert the delegate into an expression:

dynamic config = JsonConvert.DeserializeObject(configJsonString);
var typeName = config.type.ToString(); // clause expects a string, just separating it out for readability

// the guts of your clause --
// we'll turn this into an expression with o => wrapper(o)
Func<TEntity, bool> wrapper = (n => n.Name == typeName);

// wrap to expression and use as regular clause
var expectedType = repository.Where(o => wrapper(o)).FirstOrDefault();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文