Linq用匿名方法(Linq with anonymous methods)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
Linq用匿名方法(Linq with anonymous methods)

我刚开始使用LINQ,我遇到了一些麻烦。

说我想做这样的事情:

IEnumerable<String[]> = from s in listOfStrings where () => { int sum = 0; for (int i=0; i<s.Length(); i++) { sum += s[i]; } return sum < 50; } select () => { String[] t = new String[s.Length()]; for (int i=0; i<s.Length(); i++) { t[i] = s[i].toString(); } return t; }

基本上我想从listOfStrings中的字符串中获取一个字符数组作为字符串值,其总和小于50。

这只是一个例子,很难想到一个更无用的函数吧,我只是想知道如何在linq中的lambda函数中执行东西,而不需要创建一个新的函数。

谢谢!

I'm just getting started with LINQ, and I'm having some troubles.

Say I wanted to do something like this:

IEnumerable<String[]> = from s in listOfStrings where () => { int sum = 0; for (int i=0; i<s.Length(); i++) { sum += s[i]; } return sum < 50; } select () => { String[] t = new String[s.Length()]; for (int i=0; i<s.Length(); i++) { t[i] = s[i].toString(); } return t; }

Basically I want to get an array of characters as string values in from strings in listOfStrings that have a sum smaller than 50.

This is just an example though, It would be hard to think of a more useless function huh, I'm just trying to find out how to execute stuff in lambda functions within linq, without making a new function to do it.

Thanks!

最满意答案

如果使用方法链接语法而不是查询语法(即listOfStrings.Where(...) ),则可以将lambda放在那里。

If you use the method-chaining syntax instead of the query syntax (i.e. listOfStrings.Where(...)), you can stick the lambda in there.

更多推荐

本文发布于:2023-04-12 20:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a9264db32153a12bd34e532ca6ad1fb0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Linq   methods   anonymous

发布评论

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

>www.elefans.com

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