posts - 23, comments - 17, trackbacks - 0, articles - 1
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

linq

Posted on 2008-03-31 11:36 yuanws 阅读(151) 评论(5)  编辑 收藏
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),还有就是做存储过程

Feedback

#1楼    回复  引用  查看    

2008-03-31 14:01 by Clark Zheng      
lin什么?

#2楼    回复  引用  查看    

2008-03-31 14:31 by adow      
ling!!!???

#3楼 [楼主]   回复  引用  查看    

2008-03-31 17:02 by yuanws      
@adow 是linq啊

#4楼    回复  引用  查看    

2008-04-01 09:12 by adow      
--引用--------------------------------------------------
yuanws: @adow 是linq啊

--------------------------------------------------------
不容易,你终于写对了

#5楼 [楼主]   回复  引用  查看    

2008-04-02 10:53 by yuanws      
呵呵,不小心写错了

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-04-01 13:36 编辑过


相关链接: