博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq高级查询
阅读量:5963 次
发布时间:2019-06-19

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

模糊查询()包含

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
uie = con.Users;Repeater1.DataSource = uie;Repeater1.DataBind();Num.Text = uie.Count().ToString();

或者

List
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" %>
用户名 密码 昵称 性别 民族 班级 生日 操作
<%#Eval("usename") %> <%#Eval("password") %> <%#Eval("nick") %> " /> <%#Eval("Nationname") %> <%#Eval("Classname") %> <%#Eval("birthdayStr") %>
修改
删除
当前第
界面
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
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
Unick = con.Users.AsQueryable(); IQueryable
Uclass = con.Users.AsQueryable(); IQueryable
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
ListA = new List
();List
ListB = new List
();List
ListResult = new List
(); 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

你可能感兴趣的文章
【原创】Oracle RAC原理和安装
查看>>
东哥读书小记 之 《MacTalk人生元编程》
查看>>
《随机出题软件》&《随机分队软件》源码(Windows API)
查看>>
python 文件及文件夹操作
查看>>
Android自定义ListView的Item无法响应OnItemClick的解决办法
查看>>
Building Apps for Windows Phone 8.1教程下载地址整理
查看>>
移动Web—CSS为Retina屏幕替换更高质量的图片
查看>>
[Linux 性能检测工具]SAR
查看>>
JS 运行、复制、另存为 代码。
查看>>
一个经典编程面试题的“隐退”
查看>>
阿里公共DNS 正式发布了
查看>>
Java抓取网页数据(原网页+Javascript返回数据)
查看>>
[转载] 推荐的C++书籍以及阅读顺序
查看>>
EasyUI基础入门之Pagination(分页)
查看>>
ORACLE中CONSTRAINT的四对属性
查看>>
python 迭代器 生成器
查看>>
dorado基本事件样例
查看>>
Unity寻路的功能总结
查看>>
Python访问PostGIS(建表、空间索引、分区表)
查看>>
quick-cocos2d-x开发环境Lua for IntelliJ IDEA的安装
查看>>