linq查询

Linq给我们带来了很多的便利,不必时时刻刻写sql,可以让一个不懂T-SQL的程序猿也能实现简单编程,但是linq在效率上确实比不上SQL。数据量越大,效率问题就明显。

一般查询可以这样写

var v = (from c in en.T_AQI
                         where c.DatState.Equals("2") && c.SiteID == siteID
                         orderby c.DataTime descending  //排序
                         select c).Take(30).ToList();//Take和top的作用一样。ToList()就不用说了

也可以指定列查询

var v = (from c in en.T_AQI
                         where c.DatState.Equals("2") && c.SiteID == siteID
                         orderby c.DataTime descending  
                         select new {name = st.Name, age= st.Age}).Take(30).ToList();//列名是自己随意写的

也可以链接查询

 

var v = (from c in en.T_AQI
         from u in r.TabUser
                         where c.DatState.Equals("2") && c.SiteID == siteID
                         orderby c.DataTime descending  
                         select new{u.Name,c.Age,...}).Take(30).ToList();  //这个是多对多查询

 

var v = (from c in en.T_AQI
         from u in en.T_AQI
                         where c.DatState.Equals("2") && c.SiteID == c.SiteID
                         orderby c.DataTime descending  
                         select new{name=c.Name,age=c.Age}).Take(30).ToList();  //这个是自连接查询

 

var v = (from c in en.User
            join u in en.Role on c .RoleId equals u.ID into userlist
                         where c.DatState.Equals("2") && c.SiteID == c.SiteID
                         orderby c.DataTime descending  
                         select new{c.Name,c.Age,u.RoleName}).Take(30).ToList();  //这个是双向接查询

 

 

var v = (from c in en.T_AQI
         from u in r.TabUser
                         where c.DatState.Equals("2") && c.SiteID == siteID
                         orderby c.DataTime descending  
                         select u).Take(30).ToList();  //这个是一对多查询
posted @ 2014-05-08 10:19  祁临芯  阅读(600)  评论(3编辑  收藏  举报
友情链接:初心商城