public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult>
(this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int,TResult> selector);
Type Parameters
TSource
The type of the elements of source.
TResult
The type of the value returned by selector.
Parameters
source
IEnumerable<TSource>
A sequence of values to invoke a transform function on.
selector
Func<TSource,Int32,TResult>
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
Returns
IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.
Exceptions
ArgumentNullException
source or selector is null.
Example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
"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}
*/
}
}
}
No comments:
Post a Comment