1.简单查询
- //创建linqdb上下文事例
- LinqDBDataContext DB = new LinqDBDataContext();
- //创建linqdb上下文事例
- var query = from user in DB.UserInfo
- where user.ID < 5
- select user;
- //指定数据源
- GridView1.DataSource = query;
- //绑定数据源
- GridView1.DataBind();
2.聚合查询
- //创建linqdb上下文事例
- LinqDBDataContext DB = new LinqDBDataContext();
- var query = from product in DB.Product
- where product.Price == DB.Product.Max(p => p.Price)
- select product;
- GridView1.DataSource = query;
- GridView1.DataBind();
3.复杂查询
- //创建linqdb上下文事例
- LinqDBDataContext DB = new LinqDBDataContext();
- //创建linqdb上下文事例
- var query = from user in DB.UserInfo
- join role in DB.UserRole on user.ID equals role.UserID
- where user.ID < 5 && user.Username.Length >= 3
- select new { ID = user.ID, Name = user.Username, Role = role.Role.RoleName };
- //指定数据源
- GridView1.DataSource = query;
- //指定数据源
- GridView1.DataBind();
4.分组查询
- //创建linqdb上下文事例
- LinqDBDataContext DB = new LinqDBDataContext();
- //linq分组
- var query = from res in
- (from user in DB.UserInfo
- join order in DB.Order on user.ID equals order.UserId
- select new { ID = user.ID, Name = user.Username }
- )
- group res by res.ID;
- foreach (var item in query)
- {
- //创建Gv
- GridView gridView = new GridView();
- gridView.DataSource = item;
- gridView.DataBind();
- //添加html元素
- Page.Form.Controls.Add(gridView);
- }