Lambda表达式问题

发布于 2024-10-13 06:04:31 字数 579 浏览 3 评论 0原文

string[] fruits = { "apple", "banana", "mango", "orange", 
                      "passionfruit", "grape" };

var query =
    fruits.Select((fruit, index) =>
                      new { index, str = fruit.Substring(0, index) });

foreach (var obj in query)
{
    Console.WriteLine("{0}", obj);
}

/*
 This code produces the following output:

 {index=0, str=}
 {index=1, str=b}
 {index=2, str=ma}
 {index=3, str=ora}
 {index=4, str=pass}
 {index=5, str=grape}
*/

有人可以解释一下,“索引”如何在这里关联为元素的数组索引吗?

比如说,我需要一个查询,而不是第一个字母返回整个对象(在本例中为字符串)+关联索引。

string[] fruits = { "apple", "banana", "mango", "orange", 
                      "passionfruit", "grape" };

var query =
    fruits.Select((fruit, index) =>
                      new { index, str = fruit.Substring(0, index) });

foreach (var obj in query)
{
    Console.WriteLine("{0}", obj);
}

/*
 This code produces the following output:

 {index=0, str=}
 {index=1, str=b}
 {index=2, str=ma}
 {index=3, str=ora}
 {index=4, str=pass}
 {index=5, str=grape}
*/

Could somebody explain, how "index" is associated here as array index of the elements?

Say, I need a query that instead of first letter returns me the whole object (string in this case) + associated index.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

眼藏柔 2024-10-20 06:04:31

index 变量只是一个计数器,当您迭代 fruits 列表时,它会从 0 开始递增。在此示例中,当您迭代 fruits 时,indexfruitsfruit 的位置之间存在自然关系> 一次一个元素。

我不确定您关于访问“整个对象”的问题。您已经可以访问此内容:

var query = fruits.Select((fruit, index) => new { index, fruit });

当您迭代时,fruit 引用 fruits 中的当前元素。

The index variable is simply a counter that increments from 0 as you iterate through the list of fruits. In this example there is a natural relationship between index and the position of fruit in fruits as you are iterating through fruits one element at a time.

I'm not sure about your question regarding access to 'the whole object'. You already have access to this:

var query = fruits.Select((fruit, index) => new { index, fruit });

fruit refers to the current element in fruits as you iterate through it.

星星的轨迹 2024-10-20 06:04:31

要在每种情况下返回整个字符串,只需修改查询即可:

var query =
    fruits.Select((fruit, index) =>
                  new { index, str = fruit });

index 就是数组元素索引。

To return the whole string in each case just modify the query thus:

var query =
    fruits.Select((fruit, index) =>
                  new { index, str = fruit });

index is just that, the array element index.

蓝咒 2024-10-20 06:04:31

不太确定你在问什么,但尝试一下:

string[] fruits = { "apple", "banana", "mango", "orange", 
                      "passionfruit", "grape" };

var query =
    fruits.Select((fruit, index) =>
                      new { index, str = fruit });

foreach (var obj in query)
{
    Console.WriteLine("{0}", obj);
}

Index 在 Select 的重载中使用来描述 lambda 当前正在迭代的对象的索引。

Not quite sure what you're asking but try:

string[] fruits = { "apple", "banana", "mango", "orange", 
                      "passionfruit", "grape" };

var query =
    fruits.Select((fruit, index) =>
                      new { index, str = fruit });

foreach (var obj in query)
{
    Console.WriteLine("{0}", obj);
}

Index is used in an overload of Select to describe the index of the object your lambda is currently iterating over.

听,心雨的声音 2024-10-20 06:04:31

这就是 Select 的特定重载方式工作原理:“函数的第二个参数表示源元素的索引”

如果你想要整个字符串,那么你可以这样做:

var query = fruits.Select((fruit, index) => new { index, str = fruit });

That's just how that particular overload of Select works: "the second parameter of the function represents the index of the source element".

If you want the entire string then you can do something like this:

var query = fruits.Select((fruit, index) => new { index, str = fruit });
爱人如己 2024-10-20 06:04:31

lambda 表达式将第一个变量名称填充为项目本身,将第二个变量名称填充为索引。

因此,如果您有 (fruit,index) 则:

fruit = 数据对象。

索引 = 数组中的索引。

The lambda expression populates the first variable name as the item itself, and the second as the index.

So if you have (fruit,index) then:

fruit = The data object.

index = The index in the array.

满栀 2024-10-20 06:04:31

至于你的第一个问题,它是 Select 的重载。请参阅:http://msdn.microsoft.com/en-us/library/bb534869 .aspx

As for your first question, it is an overload for Select. See: http://msdn.microsoft.com/en-us/library/bb534869.aspx

你又不是我 2024-10-20 06:04:31

也许分解这个表达式的作用将有助于您理解它:

fruits.Select((fruit, index) =>
                  new { index, str = fruit.Substring(0, index) });

Select(...) = 根据输入,返回其中所示的输出。

(fruit, index) = 将选定的水果分配给变量 fruit,并将索引(Enumerable 中的位置)分配给 index。如前所述,这只是您可用的一个重载(选项)。如果您不关心索引值,只需省略它即可。

=> = 返回以下值

new { ... } = 创建匿名类型的实例。该类型有两个属性:indexstrindex 的值将是变量indexstr 的值将是水果上子字符串的结果。

因此,如果您只想要水果,可以像这样重写它:

fruits.Select(fruit => fruit); 

如果您仍然想要带有水果全名的索引:

fruits.Select((fruit, index) =>
                  new { index, str = fruit});

Select 对于返回与输入不同的信息集很有用。

举例来说,使用稍微复杂的场景:

例如,如果您有这样的类:

public class Customer { 
 public int Id {get; set;}
 public string Name { get; set;}
 public List<Order> Orders { get; set;} 
} 

public class Order { 
 public int Id { get;set;} 
 public double TotalOrderValue { get;set}
} 

您可以使用一个简单的 Select 语句来返回客户,以及该客户曾经订购的总金额:

 var customersTotalSpend = customers.Select(
      customer => 
      new { 
            customer, 
            TotalSpend = customer.Orders.Select(order => order.TotalOrderValue).Sum()
           }); 

然后我们可以做一些事情如果我们愿意的话,可以使用 TotalSpend 值,例如获得 10 个最大的消费者:

var biggestCustomers = customersTotalSpend.OrderByDescending(customerSpend=> customer.TotalSpend).Take(10); 

现在这有意义吗?

Perhaps breaking down what this expression does will help you understand it:

fruits.Select((fruit, index) =>
                  new { index, str = fruit.Substring(0, index) });

Select(...) = With the input, return an output as indicated within.

(fruit, index) = Assign the selected fruit to the variable fruit, and the index (position in Enumerable) into index. As mentioned, this is simply one overload (option) available to you. If you don't care about the index value, simply omit it.

=> = return the following value

new { ... } = Create an instance of an anonymous type. This type will have two properties: index, and str. The value of index will be the variable index. the value of str will be the result of the substring on the fruit.

So, if you simply want the fruit, you could rewrite it like so:

fruits.Select(fruit => fruit); 

If you still want the index, with the full name of the fruit:

fruits.Select((fruit, index) =>
                  new { index, str = fruit});

Select is useful for returning a different set of information from what the input was.

By way of example using a slightly more complex scenario:

For instance, if you had classes like so:

public class Customer { 
 public int Id {get; set;}
 public string Name { get; set;}
 public List<Order> Orders { get; set;} 
} 

public class Order { 
 public int Id { get;set;} 
 public double TotalOrderValue { get;set}
} 

You could use a simple Select statement to return the customer, and the sum of how much that customer has ever ordered:

 var customersTotalSpend = customers.Select(
      customer => 
      new { 
            customer, 
            TotalSpend = customer.Orders.Select(order => order.TotalOrderValue).Sum()
           }); 

We could then do something with that TotalSpend value if we wanted, eg getting the 10 biggest spenders:

var biggestCustomers = customersTotalSpend.OrderByDescending(customerSpend=> customer.TotalSpend).Take(10); 

Does that make sense now?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文