using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Data.Linq.SqlClient;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable DataTable1 = new DataTable(); DataRow row = DataTable1.NewRow(); DataTable1.Columns.Add("编号"); DataTable1.Columns.Add("姓名");
var data3result = (from c in DataTable1.AsEnumerable() select c).Union( from cc in DataTable2.AsEnumerable() select cc ); //GridView1.DataSource = data3result.CopyToDataTable<DataRow>(); //GridView1.DataBind();
/*Take*/ /*说明:获取集合的前n个元素;延迟。即只返回限定数量的结果集。*/ /*ascending是升序(默认);descending是降序;*/ DataClassesDataContext dcdc = new DataClassesDataContext(); var data6result = (from c in dcdc.city orderby c.city_id ascending select c ).Take(2); //GridView1.DataSource = data6result; //GridView1.DataBind();
/*Skip*/ /*说明:跳过集合的前n个元素;延迟。即我们跳过给定的数目返回后面的结果集。*/ DataClassesDataContext dcdcskip = new DataClassesDataContext(); var data7result = (from c in dcdc.city orderby c.city_id ascending select c).Skip(2); //GridView1.DataSource = data7result; //GridView1.DataBind();
/*Paging(分页)操作*/ /*适用场景:结合Skip和Take就可以实现对数据分页操作*/ /*1.索引*/ /*语句描述:使用Skip和Take运算符进行分页,跳过前3条记录,然后返回接下来的2条记录*/ DataClassesDataContext dcdcpageing = new DataClassesDataContext(); var data9result = (from c in dcdc.city orderby c.city_id ascending select c).Skip(3).Take(2); //GridView1.DataSource = data9result; //GridView1.DataBind();
/*按唯一键排序*/ /*语句描述:使用Where子句和Take运算符进行分页,首先筛选得到3以上的vcity_id,然后按ProductID排序,最后 取前2条数据。*/ DataClassesDataContext dcdcpageing1 = new DataClassesDataContext(); var data9result1 = (from c in dcdc.city where c.city_id>3 orderby c.city_id ascending select c).Take(2); //GridView1.DataSource = data9result1; //GridView1.DataBind(); /*SqlMethods操作*/ /*在LINQ to SQL语句中,为我们提供了SQLMETHODS操作,进一步为我们提供了方便,例如Like方法用于自定义通配*/ /*Like*/ /*自定义的通配表达式%表示零长度或任意长度的字符串;_表示一个字符;[]表示在某范围区间的一个字符;[^]表示不再某范围区间的一个字符。比如查询城市名city_name "南"开头的城市*/ DataClassesDataContext dcdclike = new DataClassesDataContext(); var data10result = from c in dcdclike.city where SqlMethods.Like(c.city_name, "南%") select c; GridView1.DataSource = data10result; GridView1.DataBind();
/* 比如查询消费者ID没有“AXOXT”形式的消费者:
var q = from c in db.Customers where !SqlMethods.Like(c.CustomerID, "A_O_T") select c; DateDiffDay 说明:在两个变量之间比较。分别有:DateDiffDay、DateDiffHour、DateDiffMillisecond、DateDiffMinute、DateDiffMonth、DateDiffSecond、DateDiffYear
var q = from o in db.Orders where SqlMethods .DateDiffDay(o.OrderDate, o.ShippedDate) < 10 select o; 语句描述:查询在创建订单后的 10 天内已发货的所有订单。
//1.创建compiled query NorthwindDataContext db = new NorthwindDataContext(); var fn = CompiledQuery.Compile( (NorthwindDataContext db2, string city) => from c in db2.Customers where c.City == city select c); //2.查询城市为London的消费者,用LonCusts集合表示,这时可以用数据控件绑定 var LonCusts = fn(db, "London"); //3.查询城市为Seattle的消费者 var SeaCusts = fn(db, "Seattle"); 语句描述:这个例子创建一个已编译查询,然后使用它检索输入城市的客户。