博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
ItelliJ IDEA开发工具使用—创建一个web项目
查看>>
solr-4.10.4部署到tomcat6
查看>>
切片键(Shard Keys)
查看>>
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
Git 常用命令
查看>>
驰骋工作流引擎三种项目集成开发模式
查看>>
SUSE11修改主机名方法
查看>>
jdk6.0 + Tomcat6.0的简单jsp,Servlet,javabean的调试
查看>>
Android:apk签名
查看>>
2(2).选择排序_冒泡(双向循环链表)
查看>>
MySQL 索引 BST树、B树、B+树、B*树
查看>>
微信支付
查看>>
CodeBlocks中的OpenGL
查看>>
短址(short URL)
查看>>
C++零基础教程(一)——何谓编程
查看>>
第十三章 RememberMe——《跟我学Shiro》
查看>>
mysql 时间函数 时间戳转为日期
查看>>
索引失效 ORA-01502
查看>>
Oracle取月份,不带前面的0
查看>>