将 Statement Lambda 放入 LINQ 查询时遇到问题
我正在尝试将一些内联工作作为语句 Lambda 注入到 LINQ 查询 select
中,如下所示...
// NOTE: mcontext.Gettype() == System.Data.Linq.DataContext
// Okay - compiles, nothing unusual
var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples
select person;
// ERROR - see below compile Error - Can I retrofit this thing?
var qPeople2 = from ME.tblPeople person in mcontext.tblPeoples
select (() => {
return person;
})();
错误:
错误2 方法名称 预期 file.cs 166 27 MigrationCore
...但是我也同样很高兴看到表达式 Lambda 首先内联工作。
注意:我知道代码示例是多余的,但我正在寻找基本概念。如果可行的话我会扩展它。
I'm trying to inject some inline work as a Statement Lambda into a LINQ query select
like so...
// NOTE: mcontext.Gettype() == System.Data.Linq.DataContext
// Okay - compiles, nothing unusual
var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples
select person;
// ERROR - see below compile Error - Can I retrofit this thing?
var qPeople2 = from ME.tblPeople person in mcontext.tblPeoples
select (() => {
return person;
})();
Error:
Error 2 Method name
expected file.cs 166 27 MigrationCore
... however I'd also be equally happy to see an Expression Lambda work inline first.
Note: I know the code sample is redundant in its effort but I'm looking for the basic concept. If it's workable I will be expanding it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询语法需要方法引用 - 它不接受 lambda,在第二个示例中,您将为其提供一个
ME.tblPeople
实例。但是,如果您使用扩展方法语法,您可以轻松实现这一点:(
我添加了递增整数作为示例,但请注意,在您枚举
qPeople3
.)附录
这仅适用于 LINQ to Objects。要将其与 LINQ to SQL 查询一起使用,需要在
Select()
调用之前调用AsEnumerable()
。注释
在这个示例中,您实际上不需要
from-in-select
构造,下面的代码片段 (AFAICT) 是相同的,但我已将其保留在上面与前面的示例相似并说明它是有效的。第二个片段将两个语句分成单独的行,也具有相同的结果。Query syntax requires a method reference - it won't accept a lambda and in your second example you're giving it a
ME.tblPeople
instance.However, if you use the extension method syntax you can achieve this easily:
(I've added the incrementing integer as an example, though note that it doesn't actually change from zero until you've enumerated
qPeople3
.)Addendum
This only works with LINQ to Objects. To use it with a LINQ to SQL query an
AsEnumerable()
call is required before theSelect()
call.Notes
You don't actually need the
from-in-select
construct for this example, the snippets below are (AFAICT) identical, but I've left it in above for similarity to the earlier examples and to illustrate that it works. The second snippet splits the two statements into separate lines, also with identical results.有两种 lambda 表达式:匿名委托和表达式树。前一种由 LINQ to Objects 使用,并允许任何有效的匿名方法主体。后一种类型由 LINQ to SQL 使用,并要求其主体是单个表达式。然后,该表达式被传递到 L2SQL 运行时并被处理成发送到服务器的 SQL。
要执行内联工作,您需要执行两个步骤:1) 使用有效的
select
表达式获取 SQL 数据,然后 2) 将该数据作为 IEnumerable<> 进行操作。使用 LINQ to Objects 来完成内联工作。这可能看起来像这样:There are two kinds of lambda expressions: anonymous delegates and expression trees. The former kind is used by LINQ to Objects, and allows any valid anonymous method body. The latter kind is used by LINQ to SQL and requires that its body be a single expression. This expression is then passed into the L2SQL runtime and manipulated into the SQL sent to the server.
To perform your inline work, you will need to use two steps: 1) get SQL data with a valid
select
expression, then 2) manipulate that data as an IEnumerable<> with LINQ to Objects to do your inline work. That could look something like this: