Linq
efEntities tc = new efEntities();
显式 LINQ 语法的一个好处是它使操作符顺序更加明确。对于前一个示例,可以很清楚的看到它从 employees 集合开始,然后调用 Where(),最后调用 Select()。
Linq To Entity表达式查询
基本查询:var ss=from s in tc.table select s;
带where的查询语句:var ss=from s in tc.table where s.id>100 select s;
简单的函数计算:(count,min,max,sum)
var ss=(from s in tc.table select s).max(s=>s.id)
var ss=(from s in tc.table select s).count();
排序order by desc/asc(
descending //倒序,
ascending //正序
)
var ss=(from s in tc.table order by id descending select s)
top(5): var ss = (from s in tc.table_ip select s).Take(5);
top(1): var ss = (from s in tc.table_ip select s).First();
跳过前面多少条数据取余下的数据
var ss = (from r in db.Am_recProScheme orderby r.rpId descending select r).Skip(10); //跳过前10条数据,取10条之后的所有数据
string sssql = "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
分页数据查询
var ss = (from r in db.Am_recProScheme where r.rpId > 10 orderby r.rpId descending select r).Skip(10).Take(10); //取第11条到第20条数据 //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
包含,类似like '%%' countains
var ss=(from r in tc.table where r.ip.Contains("196" select r)
分组 group by
var ss = (from s in tc.table_ip group s by s.city into t select new { t.Key, ipCount=t.Count() }); //方法1
var ss=(from s in tc.table_ip group s by tc.city into t select t)
foreach (var t in ss)
{ Response.Write(t.Key + "--" + t.Min(p => p.rpId)); //方法2 }
联合查询
var ss = from r in db.Am_recProScheme join w in db.Am_Test_Result on r.rpId equals w.rsId orderby r.rpId descending select r;
sql中的In
var ss = from p in db.Am_recProScheme where (new int?[] { 24, 25,26 }).Contains(p.rpId) select p; foreach (var p in ss) { Response.Write(p.Sorts); } //2 string st = "select * from Am_recProScheme where rpId in(24,25,26)";
基于方法的查询
带where的查询语句:tc.table(s=>s.id>100);
简单的函数计算:(count,min,max,sum)
var ss=tc.table.max(s=>s.id);
var ss=tc.table.count();
排序order by desc/asc
var ss= tc.table_ip.OrderBy(s=>s.ip).OrderByDescending(s=>s.id); //根据ip升序,再根据id降序
top(5): var ss = tc.table.Take(5);
top(1): var ss = tc.table.First();
跳过前面多少条数据取余下的数据
var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList();
分页数据查询
var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
包含,类似like '%%'
var ss=tc.table.where(s=>s.ip.countain("198"))
分组 group by
var ss2 = db.Am_recProScheme.GroupBy(p => p.recType); foreach (var t in ss2) { Response.Write(t.Key + "--" + t.Min(p => p.rpId));
var ss = from r in db.Am_recProScheme join w in db.Am_Test_Result on r.rpId equals w.rsId orderby r.rpId descending select r;
联合查询
var ss=
db.Am_recProScheme.join( db.Am_Test_Result,ps=>ps.id,tr=>tr.id,(ps,tr)=>ps).OrderByDescending(p => p.rpId).ToList();
)
参考:http://www.cnblogs.com/knowledgesea/p/3897665.html