将 Statement Lambda 放入 LINQ 查询时遇到问题

发布于 2024-09-10 06:40:57 字数 705 浏览 2 评论 0原文

我正在尝试将一些内联工作作为语句 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 技术交流群。

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

发布评论

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

评论(2

如梦初醒的夏天 2024-09-17 06:40:57

查询语法需要方法引用 - 它不接受 lambda,在第二个示例中,您将为其提供一个 ME.tblPeople 实例。

但是,如果您使用扩展方法语法,您可以轻松实现这一点:(

int i = 0;
var qPeople3 = (from ME.tblPeople person in mcontext.tblPeoples
                select person).Select(person => { i += 1; return person; });

我添加了递增整数作为示例,但请注意,在您枚举 qPeople3.)

附录

这仅适用于 LINQ to Objects。要将其与 LINQ to SQL 查询一起使用,需要在 Select() 调用之前调用 AsEnumerable()

注释

在这个示例中,您实际上不需要 from-in-select 构造,下面的代码片段 (AFAICT) 是相同的,但我已将其保留在上面与前面的示例相似并说明它是有效的。第二个片段将两个语句分成单独的行,也具有相同的结果。

int i = 0;
var qPeople4 = mcontext.tblPeoples.Select<ME.tblPeople,ME.tblPeople>(person => { i += 1; return person; });
int i = 0;
var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples
               select person;
var qPeople5 = qPeople1.Select(person => { i += 1; return person; });

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:

int i = 0;
var qPeople3 = (from ME.tblPeople person in mcontext.tblPeoples
                select person).Select(person => { i += 1; return person; });

(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 the Select() 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.

int i = 0;
var qPeople4 = mcontext.tblPeoples.Select<ME.tblPeople,ME.tblPeople>(person => { i += 1; return person; });
int i = 0;
var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples
               select person;
var qPeople5 = qPeople1.Select(person => { i += 1; return person; });
随风而去 2024-09-17 06:40:57

有两种 lambda 表达式:匿名委托和表达式树。前一种由 LINQ to Objects 使用,并允许任何有效的匿名方法主体。后一种类型由 LINQ to SQL 使用,并要求其主体是单个表达式。然后,该表达式被传递到 L2SQL 运行时并被处理成发送到服务器的 SQL。

要执行内联工作,您需要执行两个步骤:1) 使用有效的 select 表达式获取 SQL 数据,然后 2) 将该数据作为 IEnumerable<> 进行操作。使用 LINQ to Objects 来完成内联工作。这可能看起来像这样:

var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples
              select person;

var i = 0;
var qPeople2 = qPeople1.AsEnumerable().Select(person => {
                   i += 1;
                   return person; 
               });

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:

var qPeople1 = from ME.tblPeople person in mcontext.tblPeoples
              select person;

var i = 0;
var qPeople2 = qPeople1.AsEnumerable().Select(person => {
                   i += 1;
                   return person; 
               });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文