1、多条件查询:
并且 &&
或者 ||
var list = con.car.Where(r => r.code == "c014" || r.oil == 8.2m);
2、模糊查询
1.模糊
var list = con.car.Where(r => r.name.Contains("AT"));
2.开头
var list = con.car.Where(r => r.name.StartsWith("皇冠"));
3.结尾
var list = con.car.Where(r => r.name.EndsWith("版"));
3、统计查询
1.总个数
//var count = con.car.Count();
2.最大值
//var Max = con.car.Max(r => r.price);
3.最小值
//var Min = con.car.Min(r => r.price);
4.平均值
//var avg = con.car.Average(r => r.price);
4、分页查询
跳过,获取
int count = 5;
int pagecount = 1;
var list = con.car.Skip(pagecount * count).Take(count);
5、排序查询
升序
//var list = con.car.OrderBy(r => r.price);
降序
//var list = con.car.OrderByDescending(r => r.price);
去重:
var aaa = con.car.Select(r => r.oil).Distinct();
6、外键显示
return this.brand1.brand_name;
7、组合查询
交集
var list = list1.Intersect(list2).Intersect(list3);
-------------------------------------------------------------
var list1 = con.car.AsQueryable();
var list2 = con.car.AsQueryable();
var list3 = con.car.AsQueryable();
if (TextBox1.Text.Trim().Length > 0)
{
list1 = list1.Where(r => r.name.Contains(TextBox1.Text.Trim()));
}
if (TextBox2.Text.Trim().Length > 0)
{
list2 = list2.Where(r => r.brand1.brand_name.Contains(TextBox2.Text.Trim()));
}
if (TextBox3.Text.Trim().Length > 0)
{
list3 = list3.Where(r => r.oil == Convert.ToDecimal(TextBox3.Text.Trim()));
}
var list = list1.Intersect(list2).Intersect(list3);
Repeater1.DataSource = list;
Repeater1.DataBind();
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!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> <style type="text/css"> table { width:100%; background-color:blue; font-size:20px; font-family:微软雅黑; } #tr_head { color:white; } .tr_main { background-color:white; } </style> </head> <body> <form id="form1" runat="server"> 名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 油耗:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 价格:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="查询" /> <br /> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <tr id="tr_head"> <td>编号</td> <td>姓名</td> <td>系别</td> <td>时间</td> <td>油耗</td> <td>排量</td> <td>马力</td> <td>价格</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tr_main"> <td><%#Eval("code") %></td> <td><%#Eval("name") %></td> <td><%#Eval("brand") %></td> <td><%#Eval("time") %></td> <td><%#Eval("oil") %></td> <td><%#Eval("exhaust") %></td> <td><%#Eval("powers") %></td> <td><%#Eval("price") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </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 Default1 : System.Web.UI.Page { mydbDataContext conn = new mydbDataContext(); protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { //查询全部 //var list = conn.Car; //并且 //var list = conn.Car.Where(r=>r.Oil==8.7m&&r.Powers==130); //或者 // var list = conn.Car.Where(r => r.Oil == 8.7m || r.Powers == 130); //模糊查询 //var list = conn.Car.Where(r=>r.Name.Contains("舒适")); //已什么开头 //var list = conn.Car.Where(r => r.Name.StartsWith("宝马")); //已什么结尾 //var list = conn.Car.Where(r => r.Name.EndsWith("型")); //个数,数量 //var aa = conn.Car.Count(); //最大 //var aaaa = conn.Car.Max(r=>r.Price); //最小 //var bbb = conn.Car.Min(r=>r.Price); //平均值 // var ccc = conn.Car.Average(r=>r.Price); //分页查询 //跳过 取出 //var count = 5; //var pagecount = 2; //var list = conn.Car.Skip(count * pagecount).Take(count); //排序 //升序 //var list = conn.Car.OrderBy(r=>r.Price); //降序 // var list = conn.Car.OrderByDescending(r=>r.Price); //去重查询 //var ss = conn.Car.Select(r => r.Oil).Distinct(); //组合查询 var list1 = conn.Car.AsQueryable(); var list2 = conn.Car.AsQueryable(); var list3 = conn.Car.AsQueryable(); if(TextBox1.Text.Length>0) { list1 = conn.Car.Where(r=>r.Name.Contains(TextBox1.Text)); } if (TextBox2.Text.Length > 0) { list1 = conn.Car.Where(r => r.Oil==Convert.ToDecimal(TextBox2.Text)); } if (TextBox1.Text.Length > 0) { list1 = conn.Car.Where(r => r.Price==Convert.ToDecimal(TextBox3.Text)); } var list = list1.Intersect(list2).Intersect(list3); Repeater1.DataSource = list; Repeater1.DataBind(); } }