包含语句正文的Lambda表达式无法转换(Lambda Expression with a statement body can not been converted)

编程入门 行业动态 更新时间:2024-10-23 02:09:17
包含语句正文的Lambda表达式无法转换(Lambda Expression with a statement body can not been converted)

我试图总计跑步。

decimal currentTotal = 0; var query = test .OrderBy(i => i.Date) .Select(i => { currentTotal += i.Amount; return new { Date = i.Date, Amount = i.Amount, RunningTotal = currentTotal }; } );

我在这一行中有错误。选择.Select(i =>我怎么解决这个问题?

I'am trying to get running total.

decimal currentTotal = 0; var query = test .OrderBy(i => i.Date) .Select(i => { currentTotal += i.Amount; return new { Date = i.Date, Amount = i.Amount, RunningTotal = currentTotal }; } );

And I've got error in this line .Select(i => How could I resolve this?

最满意答案

您正在查询实体框架或LINQ2SQL:

decimal currentTotal = 0; var query = test .OrderBy(i => i.Date) .Select(i => new { Date = i.Date, Amount = i.Amount }) // Above lines executed on SQL .AsEnumerable() // Below lines executed locally .Select(i => { currentTotal += i.Amount; return new { Date = i.Date, Amount = i.Amount, RunningTotal = currentTotal }; } );

您无法在SQL Server上执行您的添加到currentTotal,因此您使用AsEnumerable()在本地执行它。

说明:

当您使用EF / LINQ2SQL进行编程时,使用IQueryable<T>构建的查询将以SQL语言进行翻译并在SQL Server上执行。 所以.OrderBy()成为ORDER BY ,所有.Where()成为WHERE等等。 很明显,如果在LINQ查询中,你将某些不能转换为SQL的东西,那么当查询翻译时你会得到一个异常。 现在,您的查询中有两个问题。

对于C#的一些限制,只有写入没有{ ... } 块的表达式(以及其他一些限制..., if ,不return ...)可以转换为Expression<Func<>> 。 由于这个原因你的程序不能被编译。

即使可以直接编译它,SQL Server也不会知道currentTotal变量的任何内容,所以在运行时会得到一个NotSupportedException异常。

解决方案:你创建一个“合法的”查询,只有那些可以由SQL执行的部分......你。选择.Select()你需要的列。 然后用.AsEnumerable()告诉EF,从.AsEnumerable()开始,它不能将查询转换为SQL,并且必须执行“本地”查询。 所以.OrderBy()和第一个.Select()将在SQL端执行,而第二个.Select() (和currentTotal )将在本地执行。

You are doing a query to Entity Framework or LINQ2SQL:

decimal currentTotal = 0; var query = test .OrderBy(i => i.Date) .Select(i => new { Date = i.Date, Amount = i.Amount }) // Above lines executed on SQL .AsEnumerable() // Below lines executed locally .Select(i => { currentTotal += i.Amount; return new { Date = i.Date, Amount = i.Amount, RunningTotal = currentTotal }; } );

You can't execute your add to currentTotal on the SQL Server, so you use AsEnumerable() to execute it locally.

Explanation:

When you program with EF/LINQ2SQL, the query you build with IQueryable<T> is translated in the SQL language and executed on the SQL Server. So the .OrderBy() becomes a ORDER BY, all the .Where() become a WHERE and so on. Clearly if in the LINQ query you put something that can't be translated to SQL, then you get an exception when the query is translated. Now, there are two problems in your query.

For some limitations of C#, only expressions written without { ... } blocks (and some other limitations... no if, no return, ...) can be translated to Expression<Func<>>. For this reason your program can't be compiled.

Even if it was possible to compile it directly, the SQL Server doesn't know anything of your currentTotal variable, so you would get a NotSupportedException at runtime.

The solution: you create a "legal" query with only the pieces that can be executed by SQL... You .Select() the columns you'll need. Then with .AsEnumerable() you tell the EF that from the .AsEnumerable() onward it mustn't translate the query to SQL, and must execute the query "locally". So the .OrderBy() and the first .Select() will be executed SQL-side, while the second .Select() (with the currentTotal) will be executed locally.

更多推荐

本文发布于:2023-07-04 16:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   语句   正文   Lambda   converted

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!