#region 常用取值 查询 List< string > isName = new List< string > { "第一" , "第二" , "第三" , "第四" } ;
List< string > result = isName. Where ( m => m == "第三" ) . ToList ( ) ; MyDBContext myDB= new MyDBContext ( ) ;
var lstUsers = myDB. UserT. Where ( e => e. userName == "lisi" ) . ToList ( ) ;
Console. WriteLine ( lstUsers. ToString ( ) ) ; #endregion #region c#查询语法糖
SqlConnection conn = new SqlConnection ( ) ;
conn. ConnectionString = "Server=localhost;Database=TestDB;Trusted_Connection=true;" ;
conn. Open ( ) ; SqlCommand command = new SqlCommand ( ) ;
command. Connection = conn;
command. CommandText = $"select * from userT where userName=' { "lisi" } '" ; SqlDataAdapter adapter = new SqlDataAdapter ( ) ;
adapter. SelectCommand = command; DataSet dataSet = new DataSet ( ) ;
adapter. Fill ( dataSet) ; conn. Close ( ) ;
#endregion #region c#修改删除新增语法糖
SqlConnection sqlupdate = new SqlConnection ( ) ;
sqlupdate. ConnectionString = "Server=localhost;Database=TestDB;Trusted_Connection=true;" ;
sqlupdate. Open ( ) ;
SqlCommand commandUpdate = new SqlCommand ( "delete userT where userName is null" , sqlupdate) ;
int count = commandUpdate. ExecuteNonQuery ( ) ; sqlupdate. Close ( ) ; #endregion #region EF6语法糖
userEntity user = new userEntity ( ) ; using ( MyDBContext mydb= new MyDBContext ( ) )
{ user = mydb. UserT. FirstOrDefault ( e => e. userName == "lisi" && e. password == "123" ) ;
}
using ( MyDBContext mydb= new MyDBContext ( ) )
{ mydb. UserT. Attach ( user) ; mydb. Entry ( user) . State = System. Data. Entity. EntityState. Modified; user. password = "666" ; mydb. SaveChanges ( ) ;
}
#endregion #region 单表查询
using ( MyDBContext myDB = new MyDBContext ( ) )
{ var query = from usert in myDB. UserTselect new userEntityNew { userName = usert. userName, password = usert. password, nickName = usert. nickName} ; List< userEntityNew> users = query. ToList ( ) ; Console. ReadLine ( ) ;
}
#endregion #region EF6联表查询
using ( MyDBContext myDB = new MyDBContext ( ) )
{ var query = from usert in myDB. UserTjoin user_scores in myDB. UserScoreson usert. ID equals user_scores. userId into utusfrom user_scores in utus. DefaultIfEmpty ( ) select new userAndscores { userName = usert. userName, nickName = usert. nickName, math = user_scores. math, english = user_scores. english, chinese = user_scores. chinese, } ; List< userAndscores> userScores = query. ToList ( ) ; Console. ReadLine ( ) ; }
using ( MyDBContext myDB = new MyDBContext ( ) )
{ var query = from usert in myDB. UserTjoin user_scores in myDB. UserScoreson usert. ID equals user_scores. userId into utusfrom user_scores in utus. OrderByDescending ( e => e. math) . Take ( 1 ) . DefaultIfEmpty ( ) select new userAndscores { userName = usert. userName, nickName = usert. nickName, math = user_scores. math, english = user_scores. english, chinese = user_scores. chinese, } ; List< userAndscores> userScores = query. ToList ( ) ; Console. ReadLine ( ) ;
}
#endregion