zoukankan      html  css  js  c++  java
  • asp.net Linq 实现分组查询

    首先我们还是先建立一个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();
  • 相关阅读:
    如何避免JavaScript的内存泄露及内存管理技巧
    【跟我一起学python吧】python chr()、unichr()和ord()
    阿里云,实力与担当并存!
    首届阿里白帽大会成功举办,用技术“肩天下”
    DataV数据可视化年度峰会——唤醒数据,看见未来
    支付宝移动端 Hybrid 解决方案探索与实践
    大数据上云第一课:(1)MaxCompute授权和外表操作躲坑指南
    函数计算支持应用中心功能
    Serverless 解惑——函数计算如何访问 MySQL 数据库
    开发函数计算的正确姿势——使用交互模式安装依赖
  • 原文地址:https://www.cnblogs.com/vip-ygh/p/3614291.html
Copyright © 2011-2022 走看看