EF常用查询语句
//方法一 Linq to Entities
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
string strSQL = "SELECT value a FROM Users as a WHERE a.ID>=10 ORDER BY a.ID DESC";
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);
}