22LINQ查询运算符返回IEnumerable<T>实例汇总
本篇体验LINQ的各种查询运算符。
先创建一个泛型方法,用来显示查询结果:
private static void DisplayQuery<T>(IEnumerable<T> query)
{
foreach (T item in query)
{
//Console.WriteLine(item.ToString());
Console.Write(item.ToString() + " ");
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
对一个集合进行操作
□ Where()对一个序列操作
● 使用Where<TSource>(this IEnumerable<TSource> , Func<TSource, bool>):
List<int> list = new List<int>()
{
1,2,3,4,5,6,7,8
};
IEnumerable<int> query = list.Where(x => x > 5);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:6 7 8
● 使用Where<TSource>(IEnumerable<TSource>,Func<TSource, int, bool>),根据数据源和元素索引返回值:
List<int> list = new List<int>()
{
1,2,3,4,5,6,7,8
};
IEnumerable<int> query = list.Where((x,index) => index>=1 && x<=3);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:2 3
□ OfType()对一个序列操作
● OfType()是对非泛型IEnumerable的扩展,所以先确定集合类型后,才可以用该方法。
● OfType()的返回类型是IEnumerable<T>泛型,所以在使用OfType()之后,可以继续链式使用其它操作符。
ArrayList list = new ArrayList();
list.Add(1);
list.Add(DateTime.Now);
list.Add("spriing is coming");
list.Add("enjoy your day");
var query = list.OfType<string>().Where(x => x.StartsWith("enjoy"));
DisplayQuery<string>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: enjoy your day
□ Cast()将非泛型序列转换成泛型序列
● 对于一个非泛型集合,就不能使用LINQ操作符:
● 使用Cast()可以将非泛型集合转换成泛型集合:
ArrayList list = new ArrayList();
list.Add("super star");
list.Add("good day");
var query = list.Cast<string>().Where(x => x.StartsWith("good"));
DisplayQuery<string>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:good day
□ OrderBy(), ThenBy(),OrderByDescending(),ThenByDescending()
关于产品和分类:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
public DateTime BuyDate { get; set; }
public decimal Price { get; set; }
public override string ToString()
{
return String.Format("编号:{0},名称:{1},类别:{2},购买时间:{3},价格:{4}", Id, Name, Category.Name, BuyDate, Price);
}
}
public static class ProductHelper
{
public static IEnumerable<Product> GetProducts()
{
Category category1 = new Category();
category1.Id = 1;
category1.Name = "数码电子";
Category category2 = new Category();
category2.Id = 2;
category2.Name = "服饰类";
return new List<Product>()
{
new Product(){Id = 1,BuyDate = new DateTime(2012,1,1),Category = category1,Name = "数码相机",Price = 800m},
new Product(){Id = 2,BuyDate = new DateTime(2012,1,3),Category = category1,Name = "录像机",Price = 900m},
new Product(){Id = 3,BuyDate = new DateTime(2012,1,2),Category = category2,Name = "体恤衫",Price = 300m},
new Product(){Id = 4,BuyDate = new DateTime(2012,1,8),Category = category2,Name = "茄克衫",Price = 600m}
};
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
● OrderBy()对某个属性排序:
var query = ProductHelper.GetProducts().OrderBy(x => x.Id);
DisplayQuery<Product>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
● OrderBy()对属性的计算值排序:
var query = ProductHelper.GetProducts().OrderBy(x => x.BuyDate.Date);
DisplayQuery<Product>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
● OrderBy()组合ThenBy()链式排序:
var query = ProductHelper.GetProducts().OrderByDescending(x => x.Category.Id).ThenBy(x => x.Price);
DisplayQuery<Product>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
注意:ThenBy()是针对IOrderedEnumerable<T>的扩展,因此不能直接在IEnumerable<T>泛型集合使用ThenBy()。
● OrderyBy()接收IComparer<T>接口类型。
创建实现IComparer<T>接口的、针对Product的比较类:
public class ProductComparer : IComparer<Product>, IEqualityComparer<Product>
{
public int Compare(Product x, Product y)
{
if (x == y)//如果类别名称相同就比较产品价格
{
return x.Price.CompareTo(y.Price);
}
else //如果类别名称不同,比较类别的编号
{
return x.Category.Id.CompareTo(y.Category.Id);
}
}
public bool Equals(Product x, Product y)
{
if (x.Category.Name == y.Category.Name)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(Product obj)
{
return obj.GetHashCode();
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
主程序:
var query = ProductHelper.GetProducts().OrderBy(x => x, new ProductComparer());
DisplayQuery<Product>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ Select()投影
var query = ProductHelper.GetProducts().Select((x, index) => new {Name = x.Name, Index = index});
foreach (var item in query)
{
Console.WriteLine("名称:{0},索引:{1}",item.Name,item.Index);
}
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
在ASP.NET MVC中,经常需要填充下拉框,可以用Select()投影出IEnumerable<SelectListItem>集合:
list.Select(x => new SelectListItem{Text = x.Name, Value = x.Code});
□ Take()和Skip()
List<int> list = new List<int>()
{
1,2,3,4,5,6,7,8
};
var query = list.Skip(7).Take(1);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:8
□ TakeWhile()和SkipWhile()
● TakeWhile()在遇到不符合条件的元素时,就停止在集合中的遍历,而Where在遇到不符合条件的元素时,会继续遍历直到结束。
int[] array = {1, 2, 10, 5, 6};
var query = array.TakeWhile(x => x <= 5);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: 1 2
● SkipWhile()会跳过符合条件的元素,直到遇到第一个不符合条件的元素,把该元素以及后面的元素返回。
int[] array = {1, 2, 10, 5, 6};
var query = array.SkipWhile(x => x <= 5);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: 10 5 6
□ Reverse()反转元素排列
int[] arr = {1, 2};
var query = arr.Reverse();
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: 2 1
□ DefaultIfEmpty()针对集合为空的处理
● 如果是空的整型集合,默认返回0
int[] array = {};
var query = array.DefaultIfEmpty();
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: 0
● 为空的整型指定默认值
int[] array = { };
var query = array.DefaultIfEmpty(100);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: 100
● 如果是空的类类型,默认值是null,不指定默认值会抛异常
Product[] arraryProducts = {};
var query = arraryProducts.DefaultIfEmpty();
DisplayQuery<Product>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
抛出异常:
● 为空的类类型指定默认值
Product[] arraryProducts = { };
Category category = new Category();
category.Id = 1;
category.Name = "酒类";
var query = arraryProducts.DefaultIfEmpty(new Product(){Id = 1,BuyDate = DateTime.Now,Category = category,Name = "贵州茅台",Price = 200m});
DisplayQuery<Product>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ Distinct()剔除重复元素
● 剔除重复的整型
int[] array = {1, 2, 2};
var query = array.Distinct();
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:1 2
● 自定义剔除规则
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return String.Format("编号:{0},名称:{1}", Id, Name);
}
}
public static class CategoryHelper
{
public static IEnumerable<Category> GetCategories()
{
return new List<Category>()
{
new Category(){Id = 1,Name = "烟类"},
new Category(){Id = 2,Name = "烟类"},
new Category(){Id = 3,Name = "酒类"}
};
}
}
public class CategoryComparer : IEqualityComparer<Category>
{
public bool Equals(Category x, Category y)
{
return x.Name == y.Name;
}
public int GetHashCode(Category obj)
{
return obj.Name.GetHashCode();
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
主程序:
IEqualityComparer<Category> customComparer = new CategoryComparer();
var query = CategoryHelper.GetCategories().Distinct(customComparer);
DisplayQuery<Category>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
注意:
public int GetHashCode(Category obj)方法中,一定要返回所比较属性的哈希值。
● 写一个适合于任何类、任何属性的泛型方法
public class PropertyComparer<T> : IEqualityComparer<T>
{
//需要比较的属性的PropertyInfo
private PropertyInfo _PropertyInfo;
//通过构造函数把需要比较的属性传进来
public PropertyComparer(string propertyName)
{
_PropertyInfo = typeof (T).GetProperty(propertyName,
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
if (_PropertyInfo == null)
{
throw new ArgumentException(string.Format("{0} 不是{1}的属性",propertyName,typeof(T)));
}
}
public bool Equals(T x, T y)
{
object xValue = _PropertyInfo.GetValue(x, null);
object yValue = _PropertyInfo.GetValue(y, null);
//如果xValue的属性值为null,那yValue的属性值也必须是null,才返回true
if (xValue == null)
return yValue == null;
return xValue.Equals(yValue);
}
public int GetHashCode(T obj)
{
object propertyValue = _PropertyInfo.GetValue(obj, null);
if (propertyValue == null)
return 0;
else
{
return propertyValue.GetHashCode();
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
主程序:
IEqualityComparer<Category> customComparer = new PropertyComparer<Category>("Name");
var query = CategoryHelper.GetCategories().Distinct(customComparer);
DisplayQuery<Category>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ GroupBy()
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return String.Format("编号:{0},名称:{1}", Id, Name);
}
}
public static class CategoryHelper
{
public static IEnumerable<Category> GetCategories()
{
return new List<Category>()
{
new Category(){Id = 1,Name = "烟类"},
new Category(){Id = 2,Name = "烟类"},
new Category(){Id = 3,Name = "酒类"}
};
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
● 通常用法
主程序:
IEnumerable<IGrouping<string,Category>> query = CategoryHelper.GetCategories().GroupBy(x => x.Name);
foreach (var item in query)
{
Console.WriteLine("<{0}>",item.Key);
foreach (var p in item)
{
Console.WriteLine(p.ToString());
}
}
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
GroupBy()的返回类型是IGrouping<TKey,TSource>的集合,该接口只提供了一个Key属性,也就是用来分组的属性值。
public interface IGrouping<out TKey,out TElement>
{
TKey Key { get; }
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
● 投影
var query = CategoryHelper.GetCategories()
.GroupBy(x => x.Name, x => new {Text = x.Id + x.Name, Value = x.Name});
foreach (var item in query)
{
Console.WriteLine("<{0}>", item.Key);
foreach (var p in item)
{
Console.WriteLine(p.Text.PadRight(20) + p.Value);
}
}
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
对多个集合进行操作
□ Intersect()获取2个集合的交集
int[] arr1 = {0, 1, 2,3};
int[] arr2 = {2, 3, 4};
var query = arr1.Intersect(arr2);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果: 2 3
□ Except()获取第一个集合中有,而第二个集合中没有的元素
int[] arr1 = {0, 1, 2,3};
int[] arr2 = {2, 3, 4};
var query = arr1.Except(arr2);
DisplayQuery<int>(query);
Console.ReadKey();
结果:0 1
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ Concat()将2个集合串联起来,不剔除重复元素
int[] arr1 = { 1, 2 };
int[] arr2 = { 2, 3 };
var query = arr1.Concat(arr2);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:1 2 2 3
□ Union()将2个集合串联起来,剔除重复元素
int[] arr1 = { 1, 2 };
int[] arr2 = { 2, 3 };
var query = arr1.Union(arr2);
DisplayQuery<int>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:1 2 3
□ Zip()合并2个集合中位置相同的元素,将2个元素的操作结果返回一个新的元素。如果两个集合的长度不相等,以长度短的为准。
int[] arr1 = {1, 2};
string[] arr2 = {"星期一", "星期二", "星期三"};
var query = arr1.Zip(arr2, (x, y) => String.Format("{0},{1}", x, y));
DisplayQuery<string>(query);
Console.ReadKey();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
总结
● 一般性的条件筛选:Where()
● 返回具体的集合类型再进行链式操作:OfType()
● 非泛型集合转换成泛型集合后再使用LINQ操作符:Cast()
● 排序、链式排序:OrderBy(), ThenBy(),实现IComparer<T>接口可以自定义排序规则
● 投影:Select()
● 返回前N个,跳过N个,分页:Take()和Skip()
● 返回符合/不符合条件,但不执行完遍历:TakeWhile()和SkipWhile()
● 反转集合元素:Reverse()
● 空集合处理:DefaultIfEmpty()
● 剔除集合中的重复元素:Distinct(),实现IEqualityComparer<Category>可自定义相等规则,针对某具体类或写一个泛型方法
● 分组以及分组后投影:GroupBy()
● 2个集合的交集:Intersect()
● 2个集合的查集:Except()
● 2个集合的串联:Concat()和Union()
● 2个集合的合并:Zip()
参考资料:
※ 《.NET之美》--张子阳,感谢写了这么好的书!
※ A Generic IEqualityComparer for Linq Distinct()
22LINQ查询运算符返回IEnumerable<T>实例汇总的更多相关文章
- 23LINQ运算符返回其它类型实例汇总
IEnumerable<T>返回其它集合类型 ToArray() ToList() ToDictionary() ToLookUp() 返回集合中的元素 □ ElementAt ...
- C#3.0新增功能09 LINQ 标准查询运算符 03 按执行方式的分类
连载目录 [已更新最新开发文章,点击查看详细] 标准查询运算符方法的 LINQ to Objects 实现主要通过两种方法之一执行:立即执行和延迟执行.使用延迟执行的查询运算符可以进一步分为两种 ...
- Javascript实例教程:querySelector()方法接受一个CSS查询并返回匹配模式的第一个子孙元素,如果没有匹配的元素则返回null。
文章简介:querySelector()方法接受一个CSS查询并返回匹配模式的第一个子孙元素,如果没有匹配的元素则返回null. querySelector()方法接受一个CSS查询并返回匹配模式的第 ...
- .NET中那些所谓的新语法之四:标准查询运算符与LINQ
开篇:在上一篇中,我们了解了预定义委托与Lambda表达式等所谓的新语法,这一篇我们继续征程,看看标准查询运算符和LINQ.标准查询运算符是定义在System.Linq.Enumerable类中的50 ...
- 十四、C# 支持标准查询运算符的集合接口
支持标准查询运算符的集合接口. System.Linq.Enumeralbe类提供的一些常用的API 来执行集合处理 1.匿名类型 2.隐匿类型的局部变量 3.集合初始化器 4.集合 5.标准查询运算 ...
- C#3.0新增功能09 LINQ 标准查询运算符 01 概述
连载目录 [已更新最新开发文章,点击查看详细] 标准查询运算符 是组成 LINQ 模式的方法. 这些方法中的大多数都作用于序列:其中序列指其类型实现 IEnumerable<T> 接 ...
- C#3.0新增功能09 LINQ 标准查询运算符 04 运算
连载目录 [已更新最新开发文章,点击查看详细] 本篇主要介绍标准查询运算符的常用运算功能. 01 对数据排序 排序操作基于一个或多个属性对序列的元素进行排序. 第一个排序条件对元素执行主要排序. ...
- .NET LINQ标准查询运算符
标准查询运算符概述 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法. 大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了 IEnumerable<T> ...
- Linq常用查询运算符
Linq一共包含五十几个查询运算符,常用的根据类型来区分一共有5类左右,这五类里面一些事在项目查询中经常用到的.不过linq运算符的命名十分规范,基本从字面意思就能猜测出来是干嘛用的,下面我们挑选一些 ...
随机推荐
- IntelliJ IDEA 2018.2.2及以下版本破解方法
破解文件下载地址:https://pan.baidu.com/s/1FKeGekyIHFUWaWi6tk2eEw =========================================== ...
- 开源的python机器学习模块
1. Scikit-learn Scikit-learn 是基于Scipy为机器学习建造的的一个Python模块,他的特色就是多样化的分类,回归和聚类的算法包括支持向量机,逻辑回归,朴素贝叶斯分类器, ...
- 20165203《Java程序设计》第七周Java学习总结
20165203<Java程序设计>第七周Java学习总结 教材学习内容总结 第11章 JDBC与MySQL数据库 MySQL数据库管理系统 MySQL数据库管理系统,简称MySQL,是世 ...
- Linux基础入门学习笔记之四
环境变量与文件查找 环境变量 变量 所谓shell变量,就是计算机中用于记录一个值(不一定是数值,也可以是字符或字符串)的符号,而这些符号将用于不同的运算处理中.通常变量与值是一对一的关系,可以通过表 ...
- vue.js学习 自定义过滤器使用(1)
在这个教程中,我们将会通过几个例子,了解和学习VueJs的过滤器.我们参考了一些比较完善的过滤器,比如orderBy 和 filterBy.而且我们可以链式调用过滤器,一个接一个过滤.因此,我们可以定 ...
- Java小实验之数据转换
看到有人问如图的程序,就去写了几行代码,顺便复习一下条件语句和ASCII码 import java.util.Scanner; public class test1 { public static v ...
- Elasticsearch介绍及安装部署
本节内容: Elasticsearch介绍 Elasticsearch集群安装部署 Elasticsearch优化 安装插件:中文分词器ik 一.Elasticsearch介绍 Elasticsear ...
- zabbix监控多实例tomcat--不使用JMX
https://blog.csdn.net/nisan892541/article/details/47727967
- day5作业购物商城+ATM
模拟实现一个ATM + 购物商城程序 1.额度 15000或自定义 2.实现购物商城,买东西加入购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.每月22号出账单,每月10号为还款日,过期未还 ...
- javascript输入验证数字方法,适合充值时输入正整数验证
说明:用于验证正整数的输入,不允许输入其他字符. html: <input type="text" id="sell_jobNum" name=" ...