博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2017-6-2 Linq 高级查询 (分页和组合查)、集合取交集
阅读量:6239 次
发布时间:2019-06-22

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

1、linq分页和组合查询:(用项目实战来解释)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
用户名:
昵称:
性别:
编号 用户名 密码 昵称 性别 生日 民族 编辑
<%#Eval("ids") %> <%#Eval("username") %> <%#Eval("password") %> <%#Eval("nickname") %> <%#Eval("sex") %> <%#Eval("birthday") %> <%#Eval("nation") %> ">修改 ">删除
当前第【
】页, 共【
】页
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(); //List
ulist = 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{    List
tlist1 = 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; }}

 

转载于:https://www.cnblogs.com/zhengqian/p/6935015.html

你可能感兴趣的文章
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>
从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
查看>>
windows查看端口占用
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
JDBC的事务
查看>>
App 卸载记录
查看>>
JavaScript变量和作用域
查看>>
开源SIP服务器加密软件NethidPro升级
查看>>
Apache Pulsar中的地域复制,第1篇:概念和功能
查看>>
python pip install 出现 OSError: [Errno 1] Operation not permitted
查看>>
从源码分析scrollTo、scrollBy、Scroller方法的区别和作用
查看>>
ObjectOutputStream和ObjectInputStream
查看>>
南京大学周志华教授当选欧洲科学院外籍院士
查看>>
马士兵教学语录
查看>>
计算机网络与Internet应用
查看>>
oracle在线迁移同步数据,数据库报错
查看>>
linux性能剖析工具
查看>>
flutter中的异步
查看>>
计算机高手也不能编出俄罗斯方块——计算机达人成长之路(16)
查看>>