yp秋水伊人

导航

高级查询:模糊、最大小值、升序降序、和、平均值。。。。

 模糊查询:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="模糊查询" />
        <asp:Button ID="Button2" runat="server" Text="开头查询" />
        <asp:Button ID="Button3" runat="server" Text="结尾查询" /><br /><br />

        价格数据统计:<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <asp:Button ID="Button4" runat="server" Text="最大值" />
        <asp:Button ID="Button5" runat="server" Text="最小值" />
        <asp:Button ID="Button6" runat="server" Text="求和" />
        <asp:Button ID="Button7" runat="server" Text="平均值" /><br /><br />

        价格排序:
        <asp:Button ID="Button8" runat="server" Text="升序" />
        <asp:Button ID="Button9" runat="server" Text="降序" /><br /><br />

后代:

 mydbDataContext conn = new mydbDataContext();
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
       
        Button1.Click += Button1_Click;//模糊查询
        Button2.Click += Button2_Click;//以。。开头查
        Button3.Click += Button3_Click;//以。。结尾查
        Button4.Click += Button4_Click;//最大值
        Button5.Click += Button5_Click;//最小值
        Button6.Click += Button6_Click;//求和
        Button7.Click += Button7_Click;//求平均值
        Button8.Click += Button8_Click;//升序(价格)
        Button9.Click += Button9_Click;//降序(价格)
        
    }

  void Button9_Click(object sender, EventArgs e)
    {//降序(价格)
        Repeater1.DataSource = conn.Car.OrderByDescending(r => r.Price).ToList();//简单方法
        Repeater1.DataBind();
    }

    void Button8_Click(object sender, EventArgs e)
    {//升序(价格)
        string p = Button8.Text;
        Repeater1.DataSource = new CarDA().Select4(p);
        Repeater1.DataBind();
    }

    void Button7_Click(object sender, EventArgs e)
    {//求平均值
        Label1.Text = new CarDA().Average();
    }

    void Button6_Click(object sender, EventArgs e)
    {//求和
        Label1.Text = new CarDA().Sum();
    }

    void Button5_Click(object sender, EventArgs e)
    {//最小值
        Label1.Text = conn.Car.Min(r => r.Price).ToString();//这个是简单的方法
    }

    void Button4_Click(object sender, EventArgs e)
    {//最大值
        Label1.Text = new CarDA().Max();
    }

    void Button3_Click(object sender, EventArgs e)
    {//以。。结尾查
        string name = TextBox1.Text;
        Repeater1.DataSource = new CarDA().Select3(name);
        Repeater1.DataBind();
    }

    void Button2_Click(object sender, EventArgs e)
    {//以。。开头查
        string name = TextBox1.Text;
        Repeater1.DataSource = new CarDA().Select2(name);
        Repeater1.DataBind();
    }

    void Button1_Click(object sender, EventArgs e)
    {//模糊查询
        string name = TextBox1.Text;   
        Repeater1.DataSource =new CarDA().Select1(name) ;
        Repeater1.DataBind();
    }

CarDA:

    mydbDataContext con = new mydbDataContext();//这句也可直接写在后台代码里,简单。
   public List<Car> Select1(string name)//模糊查的方法
    {
        return con.Car.Where(r => r.Name.Contains(name)).ToList();
    }

    public List<Car> Select2(string name)//开头查的方法
    {
        return con.Car.Where(r => r.Name.StartsWith(name)).ToList();
    }

    public List<Car> Select3(string name)//结尾查的方法
    {
        return con.Car.Where(r=>r.Name.EndsWith(name)).ToList();
    }
    public List<Car> Select4(string p)//升序
    {
    return con.Car.OrderBy(r=>r.Price).ToList();
    }
    public string Max()//最大值的方法
    {
        return con.Car.Max(r => r.Price).ToString();
    }
    

    public string Sum()//求和的方法
    {
        return con.Car.Sum(r => r.Price).ToString();
    }


    public string Average()//平均值的方法
    {
        return con.Car.Average(r => r.Price).ToString();
    }

 

posted on 2016-10-18 14:04  yp秋水伊人  阅读(225)  评论(0编辑  收藏  举报