模糊查询()包含
Repeater1.DataSource = con.Users.Where(r=>r.nick.Contains(a)).ToList();
Repeater1.DataBind();
根据开头查
Repeater1.DataSource = con.Users.Where(r => r.nick.StartsWith(a)).ToList();
Repeater1.DataBind();
根据结尾查
Repeater1.DataSource = con.Users.Where(r => r.nick.EndsWith(a)).ToList();
Repeater1.DataBind();
查询个数
IEnumerableuie = con.Users;Repeater1.DataSource = uie;Repeater1.DataBind();Num.Text = uie.Count().ToString();
或者
Listuie = con.Users.ToList();Repeater1.DataSource = uie;Repeater1.DataBind();Num.Text = uie.Count.ToString();
最大值
Num.Text = con.Users.Max(r => r.code).ToString();
Repeater1.DataBind();
最小值
Num.Text = con.Users.Min(r => r.code).ToString();
Repeater1.DataBind();
平均值:
Num.Text = con.Users.Average(r => r.code).ToString();
Repeater1.DataBind();
求和
Num.Text = con.Users.Sum(r => r.code).ToString();
Repeater1.DataBind();
升序
Repeater1.DataSource = con.Users.OrderBy(r=>r.code);
Repeater1.DataBind();
降序
Repeater1.DataSource = con.Users.OrderByDescending(r => r.code);
Repeater1.DataBind();
分页
Repeater1.DataSource = con.Users.Skip(5).Take(5);//跳过5个元素取5个元素Repeater1.DataBind();
实例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{ int count = 3; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using(Data1128DataContext con=new Data1128DataContext()) { Repeater1.DataSource = con.Users.Take(count); Repeater1.DataBind(); } } prve.Click += prve_Click; next.Click += next_Click; } //上一页 void prve_Click(object sender, EventArgs e) { int nowpage = Convert.ToInt32(txt.Text) - 1; if (nowpage < 1) return; txt.Text = nowpage.ToString(); using (Data1128DataContext con = new Data1128DataContext()) { Repeater1.DataSource = con.Users.Skip((nowpage-1)*count).Take(count); Repeater1.DataBind(); } } //下一页 void next_Click(object sender, EventArgs e) { using (Data1128DataContext con = new Data1128DataContext()) { int nowpage = Convert.ToInt32(txt.Text) + 1; int maxpage = Convert.ToInt32(Math.Ceiling((con.Users.Count() * 1.0) / count)); if (nowpage > maxpage) return; txt.Text = nowpage.ToString(); Repeater1.DataSource = con.Users.Skip((nowpage - 1) * count).Take(count); Repeater1.DataBind(); } }}
组合查询
//什么都不填,查全部 //根据所填的内容,查询对应的数据绑定到repeater上 using (Data1128DataContext con = new Data1128DataContext()) { IQueryableUi = con.Users.AsQueryable();//查询全部数据 string Nick = TextBox1.Text.Trim(); string Class = TextBox2.Text.Trim(); string Birthday = TextBox3.Text.Trim(); //判断是否需要填充查询条件 if (Nick.Length > 0) { Ui = Ui.Where(r => r.nick.Contains(Nick)); } if (Class.Length > 0) { Ui = Ui.Where(r => r.Class1.classname.Contains(Class)); } if (Birthday.Length > 0) { if (ddl.SelectedValue == "=") Ui = Ui.Where(r => Convert.ToDateTime(r.birthday).Year == Convert.ToInt32(Birthday)); if (ddl.SelectedValue == ">=") Ui = Ui.Where(r => Convert.ToDateTime(r.birthday).Year >= Convert.ToInt32(Birthday)); if (ddl.SelectedValue == "<=") Ui = Ui.Where(r => Convert.ToDateTime(r.birthday).Year <= Convert.ToInt32(Birthday)); } Repeater1.DataSource = Ui; Repeater1.DataBind(); }
using (Data1128DataContext con = new Data1128DataContext()) { IQueryableUnick = con.Users.AsQueryable(); IQueryable Uclass = con.Users.AsQueryable(); IQueryable Ubir = con.Users.AsQueryable(); string Nick = TextBox1.Text.Trim(); string Class = TextBox2.Text.Trim(); string Birthday = TextBox3.Text.Trim(); if (Nick.Length > 0) { Unick = Unick.Where(r => r.nick.Contains(Nick)); } if (Class.Length > 0) { Uclass = Uclass.Where(r => r.Class1.classname.Contains(Class)); } if (Birthday.Length > 0) { if (ddl.SelectedValue == "=") Ubir = Ubir.Where(r => Convert.ToDateTime(r.birthday).Year == Convert.ToInt32(Birthday)); if (ddl.SelectedValue == ">=") Ubir = Ubir.Where(r => Convert.ToDateTime(r.birthday).Year >= Convert.ToInt32(Birthday)); if (ddl.SelectedValue == "<=") Ubir = Ubir.Where(r => Convert.ToDateTime(r.birthday).Year <= Convert.ToInt32(Birthday)); } //取集合的交集 var Uall = Unick.Intersect(Uclass).Intersect(Ubir); Repeater1.DataSource = Uall; Repeater1.DataBind(); }
集合的交集,并集,差集,补集(去重)
ListListA = new List ();List ListB = new List ();List ListResult = new List (); ListResult = ListA.Distinct().ToList();//去重ListResult = ListA.Except(ListB).ToList();//差集ListResult= ListA.Union(ListB).ToList(); //并集ListResult = ListA.Intersect(ListB).ToList();//交集