.Net之美读书笔记5

Linq的介绍

Linq的定义

Linq的全称是集成查询语言(Language Integrated Query),它为查询各种不同的数据(内存中的集合、数据库、XML文件、Excel)源提供了一个统一的接口(包括集合中数据的操作:查询、排序、拼接等)。

Linq预备知识

泛型和委托

将委托中的参数和返回值类型泛型化,开发者可这关心函数的实现不在局限某种类型。系统提供的泛型化委托能满足我的大多需求,省去了开发者自定义委托。 如系统提供的public delegate bool Predicate<T>(T obj) Func<T,TResult> Action<T>

隐式类型与匿名类型使用

在对某集合投影(Select)时,匿名类型可省去新建过多的子类型。将在匿名类型赋予隐士变量可使业务逻辑清晰。var developer = new {Name="Jerry",Age=30}.

扩展方法

扩展方法可以通过类型实例 "."调用。扩展方法的创建

  1. 扩展方法所在的类型应为静态类
  2. 扩展方法的首个参数为需要扩展的类型,前面需要加this,这里这样这个实体参数为调用方法的实体。

匿名方法和Lambda

Lambda由匿名演变而来,也是一种方法。因编译器可以对类型推断Lambda可以省略参数类型或返回类型,直接实现业务逻辑。button.Click += (x,y)=>MessageBox.Show("Hello World")

集合

集合其实就是对数组的一个包装,为数组提供一些操作,一般集合的操作有:

  • 可以通过索引和键值来访问集合的成员
  • 可以使用for、foreach循环遍历。foreach需要集合需要实现IEnumerableIEnumerable<T>接口。
  • 可以得到Count属性
  • 拥有添加、移除等操作方法
	public class Friend
	{
		public string Name { get; set; }

		public FriendCollection GetAllFriends() 
		{
			return new FriendCollection(new Friend[]
			{
				new Friend(){Name ="关羽"},
				new Friend(){Name ="张飞"},
				new Friend(){Name ="赵云"},
				new Friend(){Name ="黄忠"},
				new Friend(){Name ="马超"}
			});
		}
	}
    	//集合
		public class FriendCollection : IEnumerable<Friend>
	{
		private Friend[] friendarray;

		public FriendCollection(Friend[] friends)
		{
			friendarray = friends;
		}

		public Friend this[int indext]
		{
			get { return friendarray[indext]; }
		}

		public int Count
		{
			get { return friendarray.Length; }
		}

		public IEnumerator<Friend> GetEnumerator()
		{
			return new FriendIterator(this);
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return new FriendIterator(this);
		}
		
		public class FriendIterator : IEnumerator<Friend>
		{
			private readonly FriendCollection friends;
			private int index;
			private Friend current;

			public FriendIterator(FriendCollection friends)
			{
				this.friends = friends;
				index = -1;
			}

			public Friend Current
			{
				get { return this.friends[index]; }
			}

			public void Dispose()
			{
			}

			object IEnumerator.Current
			{
				get { return this.friends[index]; }
			}

			public bool MoveNext()
			{
				index++;
				if (index >= friends.Count)
				{
					return false;
				}
				else
				{
					return true;
				}
			}

			public void Reset()
			{
				index = -1;
			}
		}
	}
    //调用
        Friend friend = new Friend();
        FriendCollection friendcollection = friend.GetAllFriends();
        foreach (Friend f in friendcollection)
        {
            Console.WriteLine(f.Name);
        }

Linq语法

一个简单的Linq代码

var query = from f in friendcollection
                where f.Name.Contains("张")
                orderby f.Name
                select new { Name = f.Name, Age = 30 };

    foreach (var item in query)
    {
        Console.WriteLine("Name:{0},Age:{1}", item.Name, item.Age);
    }

from声明一个范围变量f,in查询数据源,where筛选orderby排序select投影。

延迟加载

LINQ当编写一个Linq查询时,实际上并没有立即执行。当遍历集合或调用Count()Max()聚合函数时执行。

Linq查询运算符

Take()Skip()用于分页。其它还有很多主要对应Enumerable扩展方法。

原文地址:https://www.cnblogs.com/LoveTomato/p/8032597.html