• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netLinq实现分组查询

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先我们还是先建立一个person.cs类

public class person
{
  public string name
  { get; set; }
  public int age
  { get; set; }
  public string sex
  { get; set; }
}

接下来我们往这个person类添加数据:

List plist1 = new List();
  plist1.Add(new person { name = "cxx1", age = 24, sex = "" });
  plist1.Add(new person { name = "www.baidu.com", age = 25, sex = "" });
  plist1.Add(new person { name = "www.52mvc.com", age = 26, sex = "" });
  List plist2 = new List();
  plist2.Add(new person { name = "cxx1", age = 24, sex = "" });
  plist2.Add(new person { name = "cxx2", age = 28, sex = "" });
  plist2.Add(new person { name = "cxx4", age = 27, sex = "" });
  plist2.Add(new person { name = "cxx5", age = 28, sex = "" });
  // asp.net
  var query = from person p in plist1
  join person per in plist2
  on p.name equals per.name
  select new
  {
  名称=p.name,
  性别=p.sex,
  年龄=p.age
  };
  gd2.DataSource = query;
  gd2.DataBind();

返回的结果是:

  备注:这个方法是要查找出 plist1 与 plist2 之中,name名称有相同记录的数据。
  

接下来我们来看看sql中的左连接left join 在linq中如何实现
  还是先来创建两个cs 类。

///
/// 手机列表
///
public class MobileStore
{
  public string mobId
  { set; get; }
  public string mobName
  { set; get; }
}
/// 手机销售表
  ///
  public class MobileSale
  {
  public string Sid
  { set; get; }
  public string mobId
  { set; get; }
  public string mobName
  { set; get; }
  public string price
  { set; get; }
  }
  List listStore = new List();
  listStore.Add(new MobileStore { mobId = "1", mobName = "N86" });
  listStore.Add(new MobileStore { mobId = "2", mobName = "N82" });
  listStore.Add(new MobileStore { mobId = "3", mobName = "N81" });
  listStore.Add(new MobileStore { mobId = "4", mobName = "N95" });
  listStore.Add(new MobileStore { mobId = "5", mobName = "N85" });
  listStore.Add(new MobileStore { mobId = "6", mobName = "N97" });
  List listSale = new List();
  listSale.Add(new MobileSale { Sid="1" ,mobId="1",mobName="N86",price="100"});
  listSale.Add(new MobileSale { Sid="2", mobId = "2", mobName = "N82",price="220" });
  listSale.Add(new MobileSale { Sid = "3", mobId = "3", mobName = "N81", price = "300" });
  var query = from MobileStore m in listStore
  join MobileSale sale in listSale
  on m.mobId equals sale.mobId into joinm
  from j in joinm.DefaultIfEmpty()
  select new
  {
  ID = m.mobId,
  名称 = m.mobName,
  价格 = j == null ? "暂无数据" : j.price,
  };
  gd.DataSource = query;
  gd.DataBind();

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET中Response.Redirect()方法深度剖析发布时间:2022-07-10
下一篇:
ASP.NETMVC搭建项目后台UI框架—1、后台主框架发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap