主页 > 知识库 > 网络编程 > ASP/.NET >

asp.net Linq 实现分组查询(2)

来源:中国IT实验室 作者:佚名 发表于:2013-07-23 17:53  点击:
/// /// 手机销售表 /// public class MobileSale { public string Sid { set; get; } public string mobId { set; get; } public string mobName { set; get; } public string price { set; get; } } List listSto

///  /// 手机销售表
  ///
  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)
0%
没帮助
(0)
0%