博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq 高级查询
阅读量:5055 次
发布时间:2019-06-12

本文共 5325 字,大约阅读时间需要 17 分钟。

 

以~结尾:

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);

"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
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(); } }

组合查询+分页查询:

 在组合查询和分页查询的时候出现了一个问题,提示XXX已释放,无法调用,问题原因是由于把查询写成了一个方法,在页面加载的时候这个方法没有给属性拓展用上,导致对属性推展部分无法查询,

"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="查询" /><br /> 当前页数:<asp:Label ID="L2" runat="server" Text="1"></asp:Label> 总页数:<asp:Label ID="L3" runat="server" Text="1"></asp:Label><br /> <asp:Button ID="Button3" runat="server" Text="上一页" /><asp:Button ID="Button6" runat="server" Text="下一页" />

int Pcount = 2;    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //绑定数据,跳过0条,取2条 using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { Repeater1.DataSource = SS(con).Take(Pcount); Repeater1.DataBind(); L3.Text = MaxP().ToString(); } //Repeater1.DataSource = con.Stu.ToList(); //Repeater1.DataBind(); } Button1.Click += Button1_Click; Button2.Click += Button2_Click; //Button4.Click += Button4_Click; //Button5.Click += Button5_Click; Button3.Click += Button3_Click; Button6.Click += Button6_Click; } void Button6_Click(object sender, EventArgs e) { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { int a = Convert.ToInt32(L2.Text) + 1; if (a > Convert.ToInt32(MaxP())) { return; } Repeater1.DataSource = SS(con).Skip((a - 1) * Pcount).Take(Pcount); Repeater1.DataBind(); L2.Text = a.ToString(); } } void Button3_Click(object sender, EventArgs e) { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { int a = Convert.ToInt32(L2.Text) - 1; if (a < 1) { return; } Repeater1.DataSource = SS(con).Skip((a - 1) * Pcount).Take(Pcount); Repeater1.DataBind(); L2.Text = a.ToString(); } } void Button2_Click(object sender, EventArgs e) { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { Repeater1.DataSource = SS(con).Take(Pcount); Repeater1.DataBind(); L2.Text = "1"; L3.Text = MaxP().ToString(); } } //void Button5_Click(object sender, EventArgs e) //{ // Response.Redirect("toupiao.aspx"); //} //void Button4_Click(object sender, EventArgs e) //{ // Response.Cookies["id2"].Expires = DateTime.Now.AddDays(-10); //} //添加 void Button1_Click(object sender, EventArgs e) { Response.Redirect("Add.aspx"); } //把组合查询封装成一个方法 public List
SS(StudentsDataClassesDataContext con) { List
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(); } } return s; } ////获取最大页数 public int MaxP() { using (StudentsDataClassesDataContext con = new StudentsDataClassesDataContext()) { return Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(SS(con).Count) / Pcount)); } }

转载于:https://www.cnblogs.com/changxiaosen/p/6943218.html

你可能感兴趣的文章
8个Python面试必考的题目,小编也被坑过 ToT
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
centos 图形界面和命令行界面切换(转载)
查看>>
Maven启用代理访问
查看>>
Primary definition
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
发送请求时params和data的区别
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
一步步学习微软InfoPath2010和SP2010--第七章节--从SP列表和业务数据连接接收数据(4)--外部项目选取器和业务数据连接...
查看>>
如何增强你的SharePoint 团队网站首页
查看>>
FZU 1914 Funny Positive Sequence(线性算法)
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
基于grunt构建的前端集成开发环境
查看>>
MySQL服务读取参数文件my.cnf的规律研究探索
查看>>
java string(转)
查看>>
__all__有趣的属性
查看>>
BZOJ 5180 [Baltic2016]Cities(斯坦纳树)
查看>>
写博客
查看>>