1. 程式人生 > >Linq高級查詢與分頁查詢

Linq高級查詢與分頁查詢

lec end context orderby ice source contains sco 平均值

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="Null"></asp:ListItem> <asp:ListItem Text="" Value="True"></asp:ListItem> <asp:ListItem Text="" Value="False"></asp:ListItem> </asp:DropDownList> </div> <div> 成績:
<asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem Text="不限" Value="Null"></asp:ListItem> <asp:ListItem Text="大於" Value=">"></asp:ListItem> <asp:ListItem Text="小於" Value="<"></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 == ">")
                { 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高級查詢與分頁查詢