Lambda表达式问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
index
变量只是一个计数器,当您迭代fruits
列表时,它会从 0 开始递增。在此示例中,当您迭代fruits
时,index
与fruits
中fruit
的位置之间存在自然关系> 一次一个元素。我不确定您关于访问“整个对象”的问题。您已经可以访问此内容:
当您迭代时,
fruit
引用fruits
中的当前元素。The
index
variable is simply a counter that increments from 0 as you iterate through the list offruits
. In this example there is a natural relationship betweenindex
and the position offruit
infruits
as you are iterating throughfruits
one element at a time.I'm not sure about your question regarding access to 'the whole object'. You already have access to this:
fruit
refers to the current element infruits
as you iterate through it.要在每种情况下返回整个字符串,只需修改查询即可:
index
就是数组元素索引。To return the whole string in each case just modify the query thus:
index
is just that, the array element index.不太确定你在问什么,但尝试一下:
Index 在 Select 的重载中使用来描述 lambda 当前正在迭代的对象的索引。
Not quite sure what you're asking but try:
Index is used in an overload of Select to describe the index of the object your lambda is currently iterating over.
这就是
Select
的特定重载方式工作原理:“函数的第二个参数表示源元素的索引”。如果你想要整个字符串,那么你可以这样做:
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:
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.
至于你的第一个问题,它是
Select
的重载。请参阅:http://msdn.microsoft.com/en-us/library/bb534869 .aspxAs for your first question, it is an overload for
Select
. See: http://msdn.microsoft.com/en-us/library/bb534869.aspx也许分解这个表达式的作用将有助于您理解它:
Select(...)
= 根据输入,返回其中所示的输出。(fruit, index)
= 将选定的水果分配给变量fruit
,并将索引(Enumerable 中的位置)分配给index
。如前所述,这只是您可用的一个重载(选项)。如果您不关心索引值,只需省略它即可。=>
= 返回以下值new { ... }
= 创建匿名类型的实例。该类型有两个属性:index
和str
。index
的值将是变量index
。str
的值将是水果上子字符串的结果。因此,如果您只想要水果,可以像这样重写它:
如果您仍然想要带有水果全名的索引:
Select 对于返回与输入不同的信息集很有用。
举例来说,使用稍微复杂的场景:
例如,如果您有这样的类:
您可以使用一个简单的 Select 语句来返回客户,以及该客户曾经订购的总金额:
然后我们可以做一些事情如果我们愿意的话,可以使用 TotalSpend 值,例如获得 10 个最大的消费者:
现在这有意义吗?
Perhaps breaking down what this expression does will help you understand it:
Select(...)
= With the input, return an output as indicated within.(fruit, index)
= Assign the selected fruit to the variablefruit
, and the index (position in Enumerable) intoindex
. 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 valuenew { ... }
= Create an instance of an anonymous type. This type will have two properties:index
, andstr
. The value ofindex
will be the variableindex
. the value ofstr
will be the result of the substring on the fruit.So, if you simply want the fruit, you could rewrite it like so:
If you still want the index, with the full name of the 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:
You could use a simple Select statement to return the customer, and the sum of how much that customer has ever ordered:
We could then do something with that TotalSpend value if we wanted, eg getting the 10 biggest spenders:
Does that make sense now?