Entity Framework常用查询语句
时间:2022-11-15 09:46:46|栏目:.NET代码|点击: 次
方法一:Linq to Entitie
var info = from p in entity.Users where p.ID >= 10 orderby p.ID descending select p; foreach (var item in info) { Console.WriteLine("ID:" + item.ID + " " + "登录名:" + item.LoginName + " " + "密码:" + item.Password); }
方法二:Lamada表达式
var info2 = entity.Users.Where<User>(p => p.ID >= 10).OrderByDescending(p => p.ID); foreach (var item in info) { Console.WriteLine("ID:" + item.ID + " " + "登录名:" + item.LoginName + " " + "密码:" + item.Password); }
方法三:Entity SQL Language
var info3 = ((IObjectContextAdapter)entity).ObjectContext.CreateQuery<User>(strSQL); foreach (var item in info) { Console.WriteLine("ID:" + item.ID + " " + "登录名:" + item.LoginName + " " + "密码:" + item.Password); }
方法四:SqlQuery
try { string strSQL2 = "SELECT * FROM Users WHERE ID>=10 ORDER BY ID DESC"; var info4 = entity.Database.SqlQuery<User>(strSQL2); foreach (var item in info) { Console.WriteLine("ID:" + item.ID + " " + "登录名:" + item.LoginName + " " + "密码:" + item.Password); } } catch (Exception ex) { Console.WriteLine(ex.Message); }