日期:2014-05-20 浏览次数:21160 次
/ 表示产品类别的集合
static IList<Catetory> cateList = new List<Catetory> { new Catetory{CategoryId=1, CategoryName="Fruit"},
new Catetory{CategoryId=2, CategoryName="Mobile"}, new Catetory{CategoryId=3, CategoryName="Software"}};
// 表示产品的集合
static IList<Product> prodList = new List<Product> { new Product{CategoryId=1, Name="Banana", ProductId= 1},
new Product{CategoryId=1, ProductId=2, Name="Apple"}, new Product{CategoryId=1, Name="Pear" , ProductId=3},
new Product{CategoryId=2, ProductId=4, Name="NOKIA"}, new Product{CategoryId=2, ProductId=5 , Name="iPhone"},
new Product{CategoryId=3, Name="Office", ProductId=6}, new Product{CategoryId=3, ProductId=7,Name="Sql SERVER"},
new Product{CategoryId=4, Name="Hardware" ,ProductId=8}};
// 下面的查询联接产品、产品类别两个集合。用CategoryId做为键进行联接,把所有的键值相等的两个集合的对象
// 找出来。形成一个新的对象。这里使用的是查询表达式语法。
var prdli = from prod in prodList
join cate in cateList on prod.CategoryId equals cate.CategoryId
select new
{
CategoryName = cate.CategoryName,
ProductName = prod.Name,
ProdctId = prod.ProductId
};