本文整理汇总了C#中System.Linq.Queryable.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Queryable.Select方法的具体用法?C# Queryable.Select怎么用?C# Queryable.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Queryable.Select方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
string[] fruits = { "apple", "banana", "mango", "orange",
"passionfruit", "grape" };
// Project an anonymous type that contains the
// index of the string in the source array, and
// a string that contains the same number of characters
// as the string's index in the source array.
var query =
fruits.AsQueryable()
.Select((fruit, index) =>
new { index, str = fruit.Substring(0, index) });
foreach (var obj in query)
Console.WriteLine("{0}", obj);
输出:
{ index = 0, str = } { index = 1, str = b } { index = 2, str = ma } { index = 3, str = ora } { index = 4, str = pass } { index = 5, str = grape }
示例2:
List<int> range =
new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Project the square of each int value.
IEnumerable<int> squares =
range.AsQueryable().Select(x => x * x);
foreach (int num in squares)
Console.WriteLine(num);
输出:
1 4 9 16 25 36 49 64 81 100