linq

1. IList<city> cc = new List<city> { new city { Name = "100", DistanceFromSeattle = 10, Country = "中国" }, new city { Name = "10", DistanceFromSeattle = 20, Country = "美国" } };          
            GridView1.DataSource = from p in cc  where p.DistanceFromSeattle>10 orderby p.DistanceFromSeattle descending select p; // aa.getData().Tables[0];
            
           GridView1.DataBind();

2. DataClasses1DataContext dc = new DataClasses1DataContext();
            GridView1.DataSource = from p in dc.dnt_users where p.username.Length > 5 orderby p.username descending select p;
            GridView1.DataBind(); 

   或者 GridView1.DataSource = dc.dnt_users .Where(p=>p.username.Length > 5);
         GridView1.DataBind(); 


   表关联   GridView1.DataSource = from p in dc.dnt_helps
                                   join m in dc.dnt_postids
                                   on p.pid equals m.pid
                                   select new
                                   { p.message, p.id, p.title, m.postdatetime };
            GridView1.DataBind();

3. 存储过程EmployeeDataContext edc = new EmployeeDataContext();
            string firstname = "alice";
            string lastname = "wange";
            this.GridView1.DataSource = edc.ProcInsertEmployee(lastname, firstname);
            //edc.ProcInsertEmployeeResult("alice", "wang");
            this.GridView1.DataBind();

4.添删改操作
     DataClasses1DataContext dc = new DataClasses1DataContext();

添加 dnt_help dp = new dnt_help(); //表dnt_help
            dp.title = "10";
            dp.message = "中国";
            dp.pid = 10;
            dp.orderby = 1;
            dc.dnt_helps.InsertOnSubmit(dp); //插入
            dc.SubmitChanges();              //提交
            BindAll(); //绑定函数

修改 var zz = from p in dc.dnt_helps where p.title == "10" select p;
            foreach (dnt_help dh in zz)
            {
                dh.title = "china";
            }
            dc.SubmitChanges();
            BindAll();

删除 var zz = from p in dc.dnt_helps where p.title == "china" select p;
            if (zz.Count<dnt_help>() > 0)
            {  
                dc.dnt_helps.DeleteOnSubmit(zz.First<dnt_help>()); (1)
                dc.SubmitChanges();  (2)
                BindAll();
            }  //批量删除需要自己做扩展,要么循环(1)+(2),还有就是做存储过程

你可能感兴趣的:(LINQ)