2017-6-2 Linq 高级查询 (分页和组合查)集合取交集

Posted Zoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-6-2 Linq 高级查询 (分页和组合查)集合取交集相关的知识,希望对你有一定的参考价值。

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        用户名:<asp:TextBox ID="txt_uname" runat="server"></asp:TextBox>
        昵称:<asp:TextBox ID="txt_nick" runat="server"></asp:TextBox>
        性别:<asp:DropDownList ID="dr_sex" runat="server">
            <asp:ListItem Text="" Value="True"></asp:ListItem>
            <asp:ListItem Text="" Value="False"></asp:ListItem>
            <asp:ListItem Text="全部" Value="null"></asp:ListItem>
           </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="查询" />
   <table style="width:100%;text-align:center;background-color:navy;">
        <tr style="color:white;">
            <td>编号</td>
             <td>用户名</td>
             <td>密码</td>
             <td>昵称</td>
             <td>性别</td>
             <td>生日</td>
             <td>民族</td>
            <td>编辑</td>
        </tr>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
         <tr style="background-color:white;">
            <td><%#Eval("ids") %></td>
             <td><%#Eval("username") %></td>
             <td><%#Eval("password") %></td>
             <td><%#Eval("nickname") %></td>
             <td><%#Eval("sex") %></td>
             <td><%#Eval("birthday") %></td>
             <td><%#Eval("nation") %></td>
             <td>
                 <a href="update.aspx?id=<%#Eval("ids") %>">修改</a>
                 <a href="delete.aspx?id=<%#Eval("ids") %>">删除</a>
             </td>
        </tr>
                </ItemTemplate>
            </asp:Repeater>
    </table>
        当前第【<asp:Literal ID="lit_nownumber" Text="1" runat="server"></asp:Literal>】页,
        共【<asp:Literal ID="lit_max" runat="server"></asp:Literal>】页
        <asp:Button ID="btn_prev" runat="server" Text="上一页" />
        <asp:Button ID="btn_next" runat="server" Text="下一页" />
    </form>
</body>
</html>
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<users> 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+"<br/>");
                //     //最大值:
                //Response.Write("最大值:" + ulist.Max(r => Convert.ToDecimal(r.password)) + "<br/>");
                //     //最小值:
                //Response.Write("最小值:" + ulist.Min(r => Convert.ToDecimal(r.password)) + "<br/>");
                //         //平均值:
                //Response.Write("平均值:" + ulist.Average(r =>Convert.ToDecimal( r.password)) + "<br/>");
                //           //求和:
                //Response.Write("求和:" + ulist.Sum(r => Convert.ToDecimal(r.password)) + "<br/>");
                //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<users> 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<users> data() 
    {
        using (masterDataContext con = new masterDataContext())
        {
            List<users> 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" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

 

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<test1> tlist1 = new List<test1>();
    List<test1> tlist2 = new List<test1>();
    List<test1> tlist3 = new List<test1>();
    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<test1> ttlist = tlist1.Intersect(tlist2).Intersect(tlist3).ToList();
    }
}

public class test1
{
    public string code { get; set; }
    public string name { get; set; }
}

 

以上是关于2017-6-2 Linq 高级查询 (分页和组合查)集合取交集的主要内容,如果未能解决你的问题,请参考以下文章

Linq高级查询与分页查询

linq高级查询

LinQ高级查询组合查询IQueryable集合类型

linq 高级查询

LINQ 高级查询

LinQ的高级查询