linq to sql

来源:互联网 发布:mac画平面图的软件 编辑:程序博客网 时间:2024/05/02 01:18
//根据输入名称获得Id
 DataClassesDataContext dc = new DataClassesDataContext();
 string instrumentId = this.txtInstrument.Text.Trim().ToString();
 //判断是否已存在
 var query = from d in dc.Instruments where d.InstrumentId.Equals(instrumentId) select new { isHave = true };
 if (query.Count() > 0)
 {
     this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该合约已存在!');</script>");
  }
  else
  {
      //新增一条记录
      Instrument instrument = new Instrument();
      instrument.InstrumentId = this.txtInstrument.Text.Trim().ToString();
      instrument.InstrumentName = this.txtInstrument.Text.Trim().ToString();
      instrument.FirstDate = DateTime.Now;
      instrument.LastDate = DateTime.Now;
      instrument.ProductId = 1;
      dc.Instruments.InsertOnSubmit(instrument);
      this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!');window.location='InstrumentInfo.aspx';</script>");
  }

 

 

    //取最大值

    int maxOrderGroupId = dc.Orders.Select(c => c.OrderGroupId).Max();
    int maxOrderGroupId = dc.Orders.Max( a => a.OrderGroupId )

   //分类统计和

    var queryNotLock = from b in dc.Orders
                        group b by b.OrderGroupId into g
                        select g;

    var q =
    from p in dc.Orders
    group p by p.OrderGroupId into g
    select new
    {
    g.Key,
    TotalVoluem = g.Sum(p => p.VolumeTotalOriginal)
    };