1、linq分页和组合查询:(用项目实战来解释)
<%@ 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 pagecount = 2; protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; btn_next.Click += btn_next_Click; btn_prev.Click += btn_prev_Click; if(!IsPostBack) { //using(masterDataContext con=new masterDataContext()) //{ Repeater1.DataSource = data().Take(pagecount); Repeater1.DataBind(); //Listulist = con.users.ToList(); //Repeater1.DataSource = ulist.Skip(0).Take(pagecount); //排序:升序 // Repeater1.DataSource = ulist.OrderBy(r=>r.ids); //排序:降序 //Repeater1.DataSource = ulist.OrderByDescending(r=>Convert.ToDecimal(r.password)); //开头: //Repeater1.DataSource = con.users.Where(r=>r.nickname.StartsWith("王")); //结尾: //Repeater1.DataSource = con.users.Where(r => r.nickname.EndsWith("六")); //模糊查(包含): //Repeater1.DataSource = con.users.Where(r=>r.nickname.Contains("a")); //个数: //Response.Write("总个数:" + ulist.Count+" "); // //最大值: //Response.Write("最大值:" + ulist.Max(r => Convert.ToDecimal(r.password)) + " "); // //最小值: //Response.Write("最小值:" + ulist.Min(r => Convert.ToDecimal(r.password)) + " "); // //平均值: //Response.Write("平均值:" + ulist.Average(r =>Convert.ToDecimal( r.password)) + " "); // //求和: //Response.Write("求和:" + ulist.Sum(r => Convert.ToDecimal(r.password)) + " "); //Repeater1.DataBind(); //} } } void btn_prev_Click(object sender, EventArgs e) { //using (masterDataContext con = new masterDataContext()) //{ // int a = Convert.ToInt32(lit_nownumber.Text) - 1; // Repeater1.DataSource = con.users.Skip((a - 1) * pagecount).Take(pagecount); // Repeater1.DataBind(); // lit_nownumber.Text = a.ToString(); //} int a = Convert.ToInt32(lit_nownumber.Text) - 1; Repeater1.DataSource = data().Skip((a - 1) * pagecount).Take(pagecount); Repeater1.DataBind(); lit_nownumber.Text = a.ToString(); } void btn_next_Click(object sender, EventArgs e) { //using (masterDataContext con = new masterDataContext()) //{ // int a = Convert.ToInt32(lit_nownumber.Text) + 1; // Repeater1.DataSource = con.users.Skip((a - 1) * pagecount).Take(pagecount); // Repeater1.DataBind(); // lit_nownumber.Text = a.ToString(); //} int a = Convert.ToInt32(lit_nownumber.Text) + 1; Repeater1.DataSource = data().Skip((a-1)*pagecount).Take(pagecount); Repeater1.DataBind(); lit_nownumber.Text = a.ToString(); } void Button1_Click(object sender, EventArgs e) { // using(masterDataContext con=new masterDataContext()) // { // List ulist = con.users.ToList(); //if(txt_uname.Text.Trim().Length>0) //{ // ulist = ulist.Where(r=>r.username.Contains(txt_uname.Text.Trim())).ToList(); //} // if(txt_nick.Text.Trim().Length>0) // { // ulist = ulist.Where(r => r.nickname.Contains(txt_nick.Text.Trim())).ToList(); // } // if(dr_sex.SelectedValue!="null") // { // ulist = ulist.Where(r => r.sex == Convert.ToBoolean(dr_sex.SelectedValue)).ToList(); // } // Repeater1.DataSource = ulist; // Repeater1.DataBind(); // } Repeater1.DataSource = data().Take(pagecount); Repeater1.DataBind(); lit_nownumber.Text = "1"; } public List data() { using (masterDataContext con = new masterDataContext()) { List ulist = con.users.ToList(); if (txt_uname.Text.Trim().Length > 0) { ulist = ulist.Where(r => r.username.Contains(txt_uname.Text.Trim())).ToList(); } if (txt_nick.Text.Trim().Length > 0) { ulist = ulist.Where(r => r.nickname.Contains(txt_nick.Text.Trim())).ToList(); } if (dr_sex.SelectedValue != "null") { ulist = ulist.Where(r => r.sex == Convert.ToBoolean(dr_sex.SelectedValue)).ToList(); } lit_max.Text = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(ulist.Count) / pagecount)).ToString(); return ulist; } } }
2、取集合交集:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page{ Listtlist1 = new List (); List tlist2 = new List (); List tlist3 = new List (); protected void Page_Load(object sender, EventArgs e) { tlist1.Add(new test1() {code="001", name="张三" }); tlist1.Add(new test1() { code = "002", name = "李四" }); tlist1.Add(new test1() { code = "003", name = "王五" }); tlist2.Add(new test1() { code = "001", name = "张三" }); tlist2.Add(new test1() { code = "004", name = "李四" }); tlist2.Add(new test1() { code = "005", name = "王五" }); tlist3.Add(new test1() { code = "001", name = "张三" }); tlist3.Add(new test1() { code = "006", name = "李四" }); tlist3.Add(new test1() { code = "003", name = "王五" }); List ttlist = tlist1.Intersect(tlist2).Intersect(tlist3).ToList(); }}public class test1{ public string code { get; set; } public string name { get; set; }}