Linq 学习

查询表达式:

1常用的Linq字句:from(要查找的数据源),select(指定查询要返回的目标数据),where(指定查询的筛选条件),orderby(升序倒叙),group(指定元素的分组字段),Join(指定多个数据源的关联方式)

1.1:from字句:from localval in dataSource

var query = from intval in array select intval+1;
           foreach (int val in query)
             {
                 Console.WriteLine("{0}", val);
             }
             Console.ReadLine();

1.2:select字句:from localval in dataSource select expression

 

expression是一个表达式,用来计算要作为查询结果的元素

 

//用数据源中所有的元素创建一个匿名类型
            var query = from intval in array select new {Intval=intval,Doublevalue=intval*1.5 };
            foreach (var item in query)
             {
                 Console.WriteLine(item);
             }
             Console.ReadLine();

1.3 where字句:from localval in dataSource where condition select expression

//用数据源中所有的元素创建一个匿名类型
            var query = from intval in array where (intval>30&&intval<80)select new {Intval=intval,Doublevalue=intval*1.5 };
            foreach (var item in query)
             {
                 Console.WriteLine(item);
             }
             Console.ReadLine();

where intval>30 where intval<80==where (intval>30&&intval<80)

 

1.4:orderby 字句:orderby expression [sortType]

expression 是要进行排序的表达式,可以是from字句中的localval。sortType是可选参数(ascending 升序)(desending 降序)

 var query = from intval in array  orderby intval descending select new {Intval=intval,Doublevalue=intval*1.5 };
            foreach (var item in query)
             {
                 Console.WriteLine(item);
             }
             Console.ReadLine();

 

 

 

 

 

 

posted @ 2013-10-29 14:51  才高远志  阅读(239)  评论(0编辑  收藏  举报