Linq测试
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="PublicControls" Namespace="PublicControls" TagPrefix="mycols" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>产品列表</title> <style type="text/css"> body{background-color: #fff;} DIV.scott{PADDING: 2px; MARGIN: 1px; TEXT-ALIGN: right; font-size:12px;} DIV.scott A {PADDING-RIGHT: 5px;PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN-RIGHT: 2px; color:#4E4639; PADDING-TOP: 2px; BORDER: rgb(167, 176, 185) 1px solid; TEXT-DECORATION: none;} DIV.scott A:hover {BORDER: rgb(167, 176, 185) 1px solid; COLOR: #638425; BACKGROUND-COLOR: #C3BCB0} DIV.scott A:active {BORDER: rgb(167, 176, 185) 1px solid; COLOR: #638425; BACKGROUND-COLOR: #C3BCB0} DIV.scott SPAN.current {BORDER: rgb(167, 176, 185) 1px solid; PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; COLOR: #fff; MARGIN-RIGHT: 2px; PADDING-TOP: 2px; BACKGROUND-COLOR: rgb(0, 62, 139)} DIV.scott SPAN.disabled {BORDER: rgb(167, 176, 185) 1px solid; PADDING-RIGHT: 3px;PADDING-LEFT: 3px; PADDING-BOTTOM: 2px; COLOR: #ccc; MARGIN-RIGHT: 2px; PADDING-TOP: 2px;} </style> <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script> <script type="text/javascript"> $(function(){ var tempColor; $("th").css("color","#4A79A3"); $("tr:even").css("backgroundColor","#ecf5fe"); $("tr:gt(0)").mouseover(function(){ tempColor = $(this).css("backgroundColor"); $(this).css("backgroundColor","#339900"); }).mouseout(function(){ $(this).css("backgroundColor",tempColor); }); }); </script> </head> <body> <%=_sbPros %> <form id="form1" runat="server"> <mycols:MccNetPager ID="MyPager1" StyleString="class='scott'" Next="下一页" Last="尾页" Pres="..." Nexts="..." Pre="上一页" First="首页" Pages="10" runat="server" Pagesize="2" OnPageIndexChange="MyPager1_PageIndexChange"> </mycols:MccNetPager> </form> </body> </html>
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.Linq; public partial class _Default : System.Web.UI.Page { ProDALDataContext dcPro = new ProDALDataContext(); public System.Text.StringBuilder _sbPros = new System.Text.StringBuilder(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) bind(); //(from e1 in dcPro.hr_product select 0).Count();共多少条数据 //var aa = from e1 in dcPro.hr_product select e1;查询所有数据 //var aa = from e1 in dcPro.hr_product select new { e1.pid, e1.title };查询pid,title两列 //var aa = from e1 in dcPro.hr_product select new { ID = e1.pid, bt = e1.title };查询pid,title两列取别名 //var aa = (from e1 in dcPro.hr_product select new { e1.title, e1.pid }).Distinct();去掉重复的pid,title数据 //var aa = from e1 in dcPro.hr_product where e1.title == "极速越野摩托" select new { e1.title, e1.pid };查询title为“极速越野摩托”的数据 //var aa = from e1 in dcPro.hr_product where e1.title == "极速越野摩托" && e1.pid == 28 select new { e1.title, e1.pid };查询title为“极速越野摩托”并且pid为28的数据 //var aa = from e1 in dcPro.hr_product where e1.oldprice >= 50 && e1.oldprice <= 100 select new { e1.title, e1.pid };查询oldprice区间在50-100的数据 //var aa = from e1 in dcPro.hr_product orderby e1.title select new { e1.title, e1.pid };数据按title排序 //var aa = from e1 in dcPro.hr_product orderby e1.title descending, e1.pid ascending select new { e1.title, e1.pid };数据按title降序pid升序 //var aa = from e1 in dcPro.hr_product where e1.title.StartsWith("产品") select new { e1.title, e1.pid };数据like‘产品’ //var aa = from e1 in dcPro.hr_product group e1 by e1.title into g select new { zg = g.Sum(e1 => e1.oldprice) };按照title分组后oldprice的和 //var aa = from e1 in dcPro.hr_product group e1 by e1.title into g where g.Count() >= 2 select new { bt = g.Key, zg = g.Sum(e1 => e1.oldprice) };按照title分组后记录数大于等于2的title,oldprice的和 //var aa = from e1 in dcPro.hr_product from e2 in dcPro.hr_productType where e1.Ptid == e2.Ptid select new { e1, e2 };同时查询两张表 //var aa = from e1 in dcPro.hr_product join e2 in dcPro.hr_productType on e1.Ptid equals e2.Ptid select new { e1, e2 };内连接查询 //var aa = from e1 in dcPro.hr_product join e2 in dcPro.hr_productType on new { id = e1.pid, tid = e1.pid } equals new { id = e2.Ptid, tid = e2.Ptid } select new { e1, e2 };内连接同时满足多个条件 //var aa = from e1 in dcPro.hr_product join e2 in dcPro.hr_productType on e1.Ptid equals e2.Ptid into e3 select new { e1, e3 };外连接查询 //var aa = (from e1 in dcPro.hr_product select new { id = e1.title }).Union(from e2 in dcPro.hr_productType select new { id = e2.name });联合数据且不重复 //var aa = (from e1 in dcPro.hr_product where e1.pid > 2 select e1).Take(10);查询前10条pid大于2的 //var aa = from e1 in dcPro.hr_product where (from e2 in dcPro.hr_productType where e2.Ptid == 2 select e2.Ptid).Contains(Convert.ToInt32(e1.Ptid)) select e1;查询ptid等于2的数据 } public void bind() { var zjls = from e1 in dcPro.hr_product select 0; int ll = zjls.Count(); _sbPros.Append("<table><tr><th>ID</th><th>名称</th></tr>"); if (MyPager1.Pageindex == 0) { var aa = (from e1 in dcPro.hr_product select e1).Take(MyPager1.Pagesize); foreach (var bb in aa) _sbPros.Append("<tr><td>" + bb.pid + "</td><td>" + bb.title + "</td></tr>"); } else { int sl = MyPager1.Pageindex * MyPager1.Pagesize; var cc = (from e1 in dcPro.hr_product select e1).Skip(sl).Take(MyPager1.Pagesize); foreach (var bb in cc) _sbPros.Append("<tr><td>" + bb.pid + "</td><td>" + bb.title + "</td></tr>"); } _sbPros.Append("</table>"); MyPager1.Count = ll; } protected void MyPager1_PageIndexChange(object sender, EventArgs e) { bind(); } }