首页 > 代码库 > Linq高级查询与分页查询

Linq高级查询与分页查询

Linq高级查询

以~开头:

r=>r.Name.StartsWith("李");

以~结尾:

r=>r.Name.EndsWith("光");

包含(模糊查询):

r=>r.Name.Contains("四");

数据总个数:

Con.Goods.Count();||Con.Users.ToList().count; 

最大值:

Con.Goods.ToList().Max(r=>r.Price);

最小值:

Con.Goods.ToList().Min(r=>r.Price);

平均值:

Con.Goods.ToList().Average(r=>r.Price);

求和:

Con.Goods.ToList().Sum(r=>r.Price);

升序:

Con.Goods.ToList().OrderBy(r=>r.Price);

降序:

Con.Goods.ToList().OrderByDescending(r=>r.Price);

////////////////////////////////////////////////////////

组合查询:

 <div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox></div>
        <div>
            性别:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Text="男和女" Value=http://www.mamicode.com/"Null"></asp:ListItem>
                <asp:ListItem Text="" Value=http://www.mamicode.com/"True"></asp:ListItem>
                <asp:ListItem Text="" Value=http://www.mamicode.com/"False"></asp:ListItem>
            </asp:DropDownList>
        </div>
        <div>
            成绩:<asp:DropDownList ID="DropDownList2" runat="server">
                <asp:ListItem Text="不限" Value=http://www.mamicode.com/"Null"></asp:ListItem>
                <asp:ListItem Text="大于" Value=http://www.mamicode.com/">"></asp:ListItem>
                <asp:ListItem Text="小于" Value=http://www.mamicode.com/"<"></asp:ListItem>
            </asp:DropDownList><asp:TextBox ID="T2" runat="server"></asp:TextBox>
        </div>
        <asp:Button ID="Button2" runat="server" Text="查询" />
 void Button2_Click(object sender, EventArgs e)
    {
        using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext())
        {
            List<Stu> s = con.Stu.ToList();
            if (T1.Text.Trim().Length > 0)
            { s = s.Where(r => r.Name.Contains(T1.Text.Trim())).ToList(); }
            if (DropDownList1.SelectedValue != "Null")
            {  s = s.Where(r => r.Sex == Convert.ToBoolean(DropDownList1.SelectedValue)).ToList();}
            if (DropDownList2.SelectedValue != "Null")
            {
                if (DropDownList2.SelectedValue =http://www.mamicode.com/= ">")
                { s = s.Where(r => r.Score > Convert.ToInt32((T2.Text.Trim()))).ToList(); }
                else
                { s = s.Where(r => r.Score < Convert.ToInt32((T2.Text.Trim()))).ToList(); }
            }
            Repeater1.DataSource = s;
            Repeater1.DataBind();
        }
    }

组合查询+分页查询:

 

Linq高级查询与分页查询