安然不哭-西坝阿超个人博客

做技术一定要一颗恒心,这样才不会半途而废。

C# ef linq操作

2018-2-26 钟建超

 TestEntities db = new TestEntities();
                TestName t = new TestName();
                t.Name = "22334";
                db.TestName.Add(t);
                db.SaveChanges();
               
                TestName t1 = db.TestName.Find(1);

                TestName t2 = db.TestName.Where(w => w.Id == 1).FirstOrDefault();
                TestName t22 = db.TestName.Where(w => w.Name.Equals("2233")).FirstOrDefault();
                TestName t23 = db.TestName.Where(w => w.Name.Equals("2233") && w.Id == 1).FirstOrDefault();

                List<TestName> list = db.TestName.ToList().OrderBy(w => w.Id).ToList();
                List<TestName> list2 = db.TestName.ToList().OrderByDescending(w => w.Id).ToList();

                string sql = "select * from TestName where id=1";
                TestName gg = db.Database.SqlQuery<TestName>(sql).FirstOrDefault();

                //无数据库表,用视图模型
                string sql2 = "select * from TestName where id=1";
                Models.ViewModel qq = db.Database.SqlQuery<Models.ViewModel>(sql2).FirstOrDefault();

                TestName v1 = db.TestName.Where(w => w.Name.Contains("a")).FirstOrDefault();

                //跳过前五行,取后面10条
                List<TestName> list4 = db.TestName.ToList().Skip(5).Take(10).ToList();

标签: c#