简体   繁体   English

Linq的Lambda声明

[英]Lambda Statements in Linq

Is it possible to do something like the following in Linq? 是否有可能在Linq中执行以下操作?

List<Group> groupsCollection = new List<Group>();
groups.Select( g => {
    String id = g.Attributes["id"].Value;
    String title = g.Attributes["title"].Value;
    groupsCollection.Add( new Group(id, title) );
} );

This is just an example. 这只是一个例子。 I know a Foreach loop would be sufficient but I was querying whether it would be possible or not. 我知道Foreach循环就足够了,但我在询问它是否可行。

I tried it, and it's saying: 我试过了,它说:

Cannot convert lambda expression to type System.Collections.IEnumerable<CsQuery.IDomObject> because it's not a delegate type

Edit: Group is my custom class. 编辑:组是我的自定义类。 Groups are a collection of CsQuery Dom Objects. 组是CsQuery Dom对象的集合。 You can think of them as a collection of html anchor elements. 您可以将它们视为html锚元素的集合。 They are IEnumerable. 它们是IEnumerable。

I think you're looking for this: 我想你正在寻找这个:

groupsCollection = groups.Select(g =>
    new Group(g.Attributes["id"].Value,
              g.Attributes["title"].Value)).ToList();

Explanation: 说明:

Select() projects each element of a sequence into a new form. Select()将序列的每个元素投影到新表单中。 (From MSDN ) (来自MSDN

It's basically a transformation function that takes one IEnumerable and transforms it, in whatever way you like, to another IEnumerable, which seems to be exactly what you want here. 它基本上是一个转换函数,它接受一个I​​Enumerable并以任何你喜欢的方式将它转换为另一个IEnumerable,这似乎正是你想要的。

Select() takes a lambda expression that returns a value ; Select()接受一个返回值的lambda表达式; it shouldn't have any sife effects. 它不应该有任何生效。

You want 你要

var groupsCollection = groups.Select(g => {
    return ...;
}).ToList();

You can use the constructor to initialize it with the query: 您可以使用构造函数使用查询对其进行初始化:

var query = groups.Select( g => {
    String id = g.Attributes["id"].Value;
    String title = g.Attributes["title"].Value;
    return new Group(id, title);
});
List<Group> groupsCollection = new List<Group>(query);

Do not modify colections in a linq-query, instead select data that can be used to modify collections. 不要修改linq查询中的colections,而是选择可用于修改集合的数据。

As you said, you know using ForEach would be relevant. 正如你所说,你知道使用ForEach是相关的。 However you are just curious if you can do it another way. 但是,如果你能以另一种方式做到这一点,你只是好奇 As others pointed out, you can use Select but the code in the {...} should return some value. 正如其他人指出的那样,您可以使用Select{...}的代码应该返回一些值。 However, you can do it this way which is better if you want to stop or break the loop depending on some condition using TakeWhile : 但是,您可以这样做,如果您想使用TakeWhile根据某些条件stopbreak循环,这样做会更好:

groups.TakeWhile( g => {
       String id = g.Attributes["id"].Value;
       String title = g.Attributes["title"].Value;
       groupsCollection.Add( new Group(id, title) );
       return true;//If you want to break the loop, just return false accordingly.
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM