LINQ系列:Linq to Object投影操作符

  投影是指在将序列中的元素转换为一个自定义形式的操作。投影操作符Select和SelectMany用于选择出赋予了适当功能的值。SelectMany操作符可以处理多个集合。

  LINQ表达式语法:

LINQ系列:Linq to Object投影操作符

1. Select

  Select操作符对单个序列或集合中的值进行投影。

1.1 原型定义

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);

1.2 查询表达式

int[] fibonacci = new int[] { , , , , , , ,  };
var result = from f in fibonacci
select f; foreach (var item in result)
{
Console.WriteLine(item);
}
IEnumerable<int>
source = new int[] { , , , , , , , },
filtered = source.Where(f => f > ),
sorted = filtered.OrderBy(f => f),
query = sorted.Select(f => f * );
List<int> list = new List<int>() { , , , ,  };
var query = from number in list
where number <
select number;

3>. 扩展方法

int[] fibonacci = new int[] { , , , , , , ,  };
var result = fibonacci.Select(f => f); foreach (var item in result)
{
Console.WriteLine(item);
}
Object[] arr = new Object[] { "String", , true, 10.0m, 'a' };
var types = arr.Select(item => item.GetType().Name); foreach (var type in types)
{
Console.WriteLine(type);
}

4>. 查询返回列

  Select可以从序列中查询返回全部列、单独列及指定的多列。

  返回单列:

using (NorthwindContext context = new NorthwindContext())
{
var products = from p in context.Products
select p.ProductName;
}
var products = context.Products
.Select(p => p.ProductName);
// using System.IO;
DirectoryInfo di = new DirectoryInfo(@"D:\");
var query = from dir in di.GetDirectories()
orderby dir.Name
select dir.Name;

  返回多列:

var products = from p in context.Products
select new { p.ProductID, p.ProductName };
var products = context.Products
.Select(p => new { p.ProductID, p.ProductName });
int[] fibonacci = new int[] { , , , , , ,  };
var expr = fibonacci.Select((f, i) => new { Index = i, Number = f });
foreach (var item in expr)
{
Console.WriteLine(item.Index + "-" + item.Number);
}
var expr = context.Products
.OrderBy(p => p.ProductID)
.Select(p => new ProductViewModel
{
ProductID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = p.UnitPrice
});
var expr = context.Products
.Select(p => new
{
ProductID = p.ProductID,
ProductName = p.ProductName,
Available = p.UnitsInStock - p.UnitsOnOrder > ? "In Stock" : "Out Of Stock"
});
var expr = context.Products
.Select(p => new
{
ProductID = p.ProductID,
ProductName = p.ProductName,
Units = new
{
p.UnitsInStock,
p.UnitsOnOrder,
Available = p.UnitsInStock - p.UnitsOnOrder > ? "In Stock" : "Out Of Stock"
}
});
var query = context.Products
.Select((p, index) => new
{
Position = index,
p.ProductName,
p.UnitPrice
});

5>. Select方法实现

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
foreach (TSource element in source)
{
yield return selector(element);
}
}

2. SelectMany

  SelectMany操作符可以将多个from字句组合起来,将每个对象的结果合并成一个序列。

1>. 原型定义

public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector);
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector);

2>. 返回全部列

var expr = from c in context.Customers
from o in c.Orders
select o;
var expr = context.Customers
.SelectMany(c => c.Orders);

3>. 返回指定列

var expr = from c in context.Customers
from o in c.Orders
select new { o.OrderID, o.OrderDate };
var expr = context.Customers
.SelectMany(c => c.Orders
.Select(o => new { o.OrderID, o.OrderDate }));
var query = context.Categories
.Where(c => c.CategoryID == )
.SelectMany(c => c.Products
.Where(p => p.CategoryID == c.CategoryID)
.Select(p => new
{
c.CategoryName,
p.ProductName,
p.UnitPrice
})
);
var expr = from c in context.Categories
from p in c.Products
where c.CategoryName == "LINQ to Object"
select p;
var query = from c in context.Categories
select new
{
CategoryID = c.CategoryID,
CategoryName = c.CategoryName,
ProductIDs = from p in c.Products
select p.ProductID
};
var expr = from c in context.Categories
from p in c.Products
select new
{
c.CategoryName,
p.ProductID,
p.ProductName
}; var expr = from c in
context.Categories
.SelectMany(c => from p in c.Products
select new
{
c.CategoryName,
p.ProductID,
p.ProductName
})
select c; var expr = context.Categories
.SelectMany(c => c.Products
.Select(p => new
{
c.CategoryName,
p.ProductID,
p.ProductName
}));

3. Let

var expr = from p in context.Products
let productInfo = p.UnitPrice + "," + p.UnitsInStock
select new { p.ProductName, productInfo };
var expr = context.Products
.Select(p => new { p, ProductInfo = p.UnitPrice + "," + p.UnitsInStock })
.Select(t => new { t.p.ProductName, t.ProductInfo }); foreach (var item in expr)
{
Console.WriteLine("{0}-{1}", item.ProductName, item.ProductInfo);
}
上一篇:Migrating an ASP.NET MVC application to ADFS authentication


下一篇:手写总结:synchronized 和 lock 区别