Linq高级查询

模糊查询()包含

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();

查询个数

IEnumerable<Users> uie = con.Users;
Repeater1.DataSource = uie;
Repeater1.DataBind();
Num.Text = uie.Count().ToString();

或者

List<Users> uie = 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" %>

<!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>
    <table style=" 100%; background-color: yellow;">
                <tr style="background-color: blue; 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 class="t-body">
                            <td><%#Eval("usename") %></td>
                            <td><%#Eval("password") %></td>
                            <td><%#Eval("nick") %></td>
                            <td>
                                <img src="<%#Eval("sexStr") %>" />
                            </td>
                            <td><%#Eval("Nationname") %></td>
                            <td><%#Eval("Classname") %></td>
                            <td><%#Eval("birthdayStr") %></td>
                            <td>
                                <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("code") %>'>修改</asp:LinkButton>
                                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CommandArgument='<%#Eval("code") %>'>删除</asp:LinkButton></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>

            </table>
        当前第<asp:Label ID="txt" runat="server" Text="1"></asp:Label><asp:Button ID="prve" runat="server" Text="上一页" /><asp:Button ID="next" runat="server" Text="下一页" />
    </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 _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())
        {
            IQueryable<Users> Ui = 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())
        {
            IQueryable<Users> Unick = con.Users.AsQueryable();
            IQueryable<Users> Uclass = con.Users.AsQueryable();
            IQueryable<Users> 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();

        }
方法二:使用集合的交集

 集合的交集,并集,差集,补集(去重)

List<string> ListA = new List<string>();
List<string> ListB = new List<string>();
List<string> ListResult = new List<string>();
 
 
 
 
ListResult = ListA.Distinct().ToList();//去重
ListResult = ListA.Except(ListB).ToList();//差集
ListResult= ListA.Union(ListB).ToList();  //并集
ListResult = ListA.Intersect(ListB).ToList();//交集
原文地址:https://www.cnblogs.com/maxin991025-/p/6381239.html