码迷,mamicode.com
首页 > 其他好文 > 详细

枚举器和linq

时间:2014-12-21 13:51:48      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

Iterator:枚举器

如果你正在创建一个表现和行为都类似于集合的类,允许类的用户使用foreach语句对集合中的成员进行枚举将会是很方便的。

我们将以创建一个简单化的List Box作为开始,它将包含一个8字符串的数组和一个整型,这个整型用于记录数组中已经添加了多少字符串。构造函数将对数组进行初始化并使用传递进来的参数填充它。

 


using
System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NETTest { /// <summary> /// Iterator:枚举器 /// 测试枚举器,继承IEnumerable,实现IEnumerator<T> GetEnumerator() /// </summary> /// <typeparam name="T"></typeparam> public class ListBox<T> : IEnumerable<T> { public List<T> Strings = new List<T>(); public ListBox(params T[] initialStrings) { foreach (T s in initialStrings) { Strings.Add(s); } } IEnumerator<T> IEnumerable<T>.GetEnumerator()//这个方法和下面的方法都必须实现 { foreach (T s in Strings) { yield return s; } }


//GetEnumerator方法使用了新的 yield 语句。yield语句返回一个表达式。yield语句仅在迭代块中出现,
//并且返回foreach语句所期望的值。那也就是,对GetEnumerator的每次调用都//将会产生集合中的下一个字符串;所有的状态管理已经都为你做好了!



        IEnumerator IEnumerable.GetEnumerator()//这个方法和上面的方法都必须实现
        {
            throw new NotImplementedException();
        }
    }
}
class Program
    {
        static void Main(string[] args)
        {
            //ProgramTest p = new ProgramTest(3);
            //p.TestString();

            ListBox<string> lb = new ListBox<string>("wea", "b", "c", "d", "e", "f", "g", "h");
            foreach (string s in lb)
            {
                Console.Write(s + " ");
            }

            Console.WriteLine();
            ListBox<int> lb1 = new ListBox<int>(4, 5, 6, 34);
            foreach (int s in lb1)
            {
                Console.Write(s + " ");
            }
            ListBox<string> lb = new ListBox<string>("wea", "b", "c", "d", "e", "f", "g", "h");
            foreach (string s in lb.Strings)//调用属性
            {
                Console.Write(s + " ");
            }

 



            Console.ReadLine();
        }
    }

LINQ的基本格式如下所示:
var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式>

group分组子句
语句格式:var str = from p in PersonList group p by p.age
group子句将数据源中的数据进行分组,在遍历数据元素时,并不像前面的章节那样直接对元素进行遍历,因为group子句返回的是元素类型为IGrouping<TKey,TElement>的对象序列,必须在循环中嵌套一个对象的循环才能够查询相应的数据元素。
在使用group子句时,LINQ查询子句的末尾并没有select子句,因为group子句会返回一个对象序列,通过循环遍历才能够在对象序列中寻找到相应的对象的元素,如果使用group子句进行分组操作,可以不使用select子句。

orderby排序子句
语句格式:var str = from p in PersonList orderby p.age select p;
orderby子句中使用descending关键字进行倒序排列
示例代码如下:
var str = from p in PersonList orderby p.age descending select p;    
orderby子句同样能够进行多个条件排序,只需要将这些条件用“,”号分割即可
示例代码如下:
var str = from p in PersonList orderby p.age descending,p.name select p;

join连接子句
在LINQ中同样也可以使用join子句对有关系的数据源或数据对象进行查询,但首先这两个数据源必须要有一定的联系
var str = from p in PersonList join car in CarList on p.cid equals car.cid select p;

var innerJoinQuery =
    from cust in customers
    join dist in distributors on cust.City equals dist.City
    select new { CustomerName = cust.Name, DistributorName = dist.Name };

 

// custQuery is an IEnumerable<IGrouping<string, Customer>>
var custQuery =
    from cust in customers
    group cust by cust.City into custGroup
    where custGroup.Count() > 2
    orderby custGroup.Key
    select custGroup;

 


string
arrays = "hua ying jie."; var s = from arra in arrays orderby arra descending select arra; List<char> s2 = (from arra in arrays orderby arra descending select arra).ToList(); char[] s6 = (from arra in arrays orderby arra descending select arra).ToArray(); IEnumerable<char> s4 = from arra in arrays orderby arra descending select arra; IEnumerable<char> s5 = arrays.OrderByDescending(n => n);//这五种实现效果相同

 

枚举器和linq

标签:

原文地址:http://www.cnblogs.com/zhao123/p/4176407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!