MethodInfo[] methods = typeof(string).GetMethods(); var result = from m in methods where m.IsStatic != true select m.Name; foreach (var r in result) { Console.WriteLine(r.ToString()); } Console.ReadLine();
var result = from m in methods where m.IsStatic != true group m by m.Name into g select new { MethodName = g.Key, Overload = g.Count() };
这里面又有一些新鲜的了——select new { MethodName = g.Key, Overload = g.Count() },先来看一个简单一些的例子:
class MyClass { public string MethodName { get; set; } public int Overload { get; set; } } class Program { MyClass mc = new MyClass { MethodName = "aaa", Overload = 2 }; }
大括号里面的叫类初始化器,省去了构造函数,在new的同时,给对象的属性赋值。
这时候再回来看看select new { MethodName = g.Key, Overload = g.Count() },跟这个类初始化器很像吧,但是它更偷懒,new一个新对象,居然连类名都不写。没错,这就叫匿名类。不用写类的声明,直接实例化。类的名字是由编译器在编译的时候自动生成的,也就是说,你在new的时候根本不知道这个类叫啥名,因此,这里就只能用var了。这就更看出var的厉害了,不仅仅是写起来方便这么简单,在用到匿名类的时候,没有类名,这时候只能用var,嘿嘿!
Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并提供了丰富的查询功能,它和Linq to xml、Linq to objects、Linq to dataset、Linq to entities等组成了强大的LINQ。
要学好LINQ查询语法,就不得不先理解C# 3.0的一些新特性,下面一一简单介绍。
隐含类型局部变量
var age = 26; var username = "zhuye"; var userlist = new [] {"a","b","c"}; foreach(var user in userlist) Console.WriteLine(user);
public class Person { public string username { get; protected set; } public int age { get; set; } public Person() { this.username = "zhuye"; } } Person p = new Person(); //p.username = "aa"; Console.WriteLine(p.username);
public class Person { public string username { get; set; } public int age { get; set; } public override string ToString() { return string.Format("username:{0} age:{1}", this.username, this.age); } } Person p = new Person() {username = "zhuye", age=26}; Console.WriteLine(p.ToString());
编译器会自动为你做setter操作,使得原本几行的属性赋值操作能在一行中完成。这里需要注意:
允许只给一部分属性赋值,包括internal访问级别
可以结合构造函数一起使用,并且构造函数初始化先于对象初始化器执行
集合初始化器
public class Person { public string username { get; set; } public int age { get; set; } public override string ToString() { return string.Format("username:{0} age:{1}", this.username, this.age); } } var persons = new List { new Person {username = "a", age=1}, new Person {username = "b", age=2}}; foreach(var p in persons) Console.WriteLine(p.ToString());
public delegate int mydg(int a, int b); public static class LambdaTest { public static int oper(this int a, int b, mydg dg) { return dg(a, b); } } Console.WriteLine(1.oper(2, (a, b) => a + b)); Console.WriteLine(2.oper(1, (a, b) => a - b));
查询句法
var persons = new List { new Person {username = "a", age=19}, new Person {username = "b", age=20}, new Person {username = "a", age=21}, }; var selectperson = from p in persons where p.age >= 20 select p.username.ToUpper(); foreach(var p in selectperson) Console.WriteLine(p);
查询句法是使用标准的LINQ查询运算符来表达查询时一个方便的声明式简化写法。该句法能在代码里表达查询时增进可读性和简洁性,读起来容易,也容易让人写对。Visual Studio 对查询句法提供了完整的智能感应和编译时检查支持。编译器在底层把查询句法的表达式翻译成明确的方法调用代码,代码通过新的扩展方法和Lambda表达式语言特性来实现。上面的查询句法等价于下面的代码:
var selectperson = persons.Where(p=>p.age>=20).Select(p=>p.username.ToUpper());
DataContext ctx = new DataContext("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); Table Customers = ctx.GetTable(); GridView1.DataSource = from c in Customers where c.CustomerID.StartsWith("A") select new {顾客ID=c.CustomerID, 顾客名=c.Name, 城市=c.City}; GridView1.DataBind();
public partial class NorthwindDataContext : DataContext { public Table Customers; public NorthwindDataContext(IDbConnection connection) : base(connection) { } public NorthwindDataContext(string connection) : base(connection) { } }
强类型数据上下文使代码更简洁:
NorthwindDataContext ctx = new NorthwindDataContext("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); GridView1.DataSource = from c in ctx.Customers where c.CustomerID.StartsWith("A") select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City }; GridView1.DataBind();
DataContext其实封装了很多实用的功能,下面一一介绍。
日志功能
using System.IO;
NorthwindDataContext ctx = new NorthwindDataContext("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true); // Append ctx.Log = sw; GridView1.DataSource = from c in ctx.Customers where c.CustomerID.StartsWith("A") select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City }; GridView1.DataBind(); sw.Close();
运行程序后在网站所在目录生成了log.txt,每次查询都会把诸如下面的日志追加到文本文件中:
SELECT [t0].[CustomerID], [t0].[ContactName], [t0].[City] FROM [Customers] AS [t0] WHERE [t0].[CustomerID] LIKE @p0 -- @p0: Input String (Size = 2; Prec = 0; Scale = 0) [A%] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
应该说这样的日志对于调试程序是非常有帮助的。
探究查询
using System.Data.Common; using System.Collections.Generic;
NorthwindDataContext ctx = new NorthwindDataContext("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); var select = from c in ctx.Customers where c.CustomerID.StartsWith("A") select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City }; DbCommand cmd = ctx.GetCommand(select); Response.Write(cmd.CommandText + " "); foreach (DbParameter parm in cmd.Parameters) Response.Write(string.Format("参数名:{0},参数值:{1} ", parm.ParameterName, parm.Value)); Customer customer = ctx.Customers.First(); customer.Name = "zhuye"; IList
NorthwindDataContext ctx = new NorthwindDataContext("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); string newcity = "Shanghai"; ctx.ExecuteCommand("update Customers set City={0} where CustomerID like 'A%'", newcity); IEnumerable customers = ctx.ExecuteQuery("select * from Customers where CustomerID like 'A%'"); GridView1.DataSource = customers; GridView1.DataBind();
前一篇文章已经说了,虽然Linq to sql能实现90%以上的TSQL功能。但是不可否认,对于复杂的查询,使用TSQL能获得更好的效率。因此,DataContext类型也提供了执行SQL语句的能力。代码的执行结果如下图:
创建数据库
testContext ctx = new testContext("server=xxx;database=testdb;uid=xxx;pwd=xxx"); ctx.CreateDatabase();
[Table(Name = "test")] public class test { [Column(IsPrimaryKey = true, IsDbGenerated = true)] public int ID { get; set; }
[Column(DbType="varchar(20)")] public string Name { get; set; } }
public partial class testContext : DataContext { public Table test; public testContext(string connection) : base(connection) { } }
testContext ctx = new testContext("server=xxx;database=testdb;uid=xxx;pwd=xxx");
public testContext(string connection) : base(connection) { }
}
这段代码在数据库中创建了名为testdb的数据库,等同于下面的脚本:
CREATE TABLE [dbo].[test]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL, CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
同时,DataContext还提供了DeleteDatabase()方法,在这里就不列举了。
使用DbDataReader数据源
using System.Data.SqlClient;
var conn = new SqlConnection("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); var ctx = new DataContext(conn); var cmd = new SqlCommand("select * from customers where CustomerID like 'A%'", conn); conn.Open(); var reader = cmd.ExecuteReader(); GridView1.DataSource = ctx.Translate(reader); GridView1.DataBind(); conn.Close();
你可能很难想象,使用Linq to sql进行数据访问会是这么简单,后台代码: public partial class _Default : System.Web.UI.Page { GuestBookDataContext ctx = new GuestBookDataContext("server=xxx;database=GuestBook;uid=xxx;pwd=xxx");
前面创建Linq to sql Classes的时候我们输入名字GuestBook,系统就为我们自动创建了GuestBookDataContext(你也可以在GuestBook.Designer.cs中找到类定义)。在绑定的时候我们使用查询句法查询留言表中所有留言,按照发表时间倒序(天哪?这是数据访问吗?好像仅仅定义了一句SQL啊)。在发表留言按钮中,我们为一个tbGuestBook赋值,然后把它加入留言表,再提交更改,就这样完成了记录的插入。
运行效果如下图:
然后,再创建一个Admin.aspx,前台代码如下:
<%# Eval("Message")%>
<%# Eval("PostTime")%> - <%# Eval("UserName")%>
'/> 管理员回复:'/> '/>
后台代码:
public partial class Admin : System.Web.UI.Page { GuestBookDataContext ctx = new GuestBookDataContext("server=xxx;database=GuestBook;uid=xxx;pwd=xxx");
var 多条件 = from c in ctx.Customers where c.Country == "France" && c.Orders.Count > 5 select new { 国家 = c.Country, 城市 = c.City, 订单数 = c.Orders.Count };
对应SQL:
SELECT [t0].[Country], [t0].[City], ( SELECT COUNT(*) FROM [dbo].[Orders] AS [t2] WHERE [t2].[CustomerID] = [t0].[CustomerID] ) AS [value] FROM [dbo].[Customers] AS [t0] WHERE ([t0].[Country] = @p0) AND ((( SELECT COUNT(*) FROM [dbo].[Orders] AS [t1] WHERE [t1].[CustomerID] = [t0].[CustomerID] )) > @p1) -- @p0: Input String (Size = 6; Prec = 0; Scale = 0) [France] -- @p1: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]
orderby
描述:查询所有没有下属雇员的雇用年和名,按照雇用年倒序,按照名正序
查询句法:
var 排序 = from emp in ctx.Employees where emp.Employees.Count == 0 orderby emp.HireDate.Value.Year descending, emp.FirstName ascending select new { 雇用年 = emp.HireDate.Value.Year, 名 = emp.FirstName };
对应SQL:
SELECT DATEPART(Year, [t0].[HireDate]) AS [value], [t0].[FirstName] FROM [dbo].[Employees] AS [t0] WHERE (( SELECT COUNT(*) FROM [dbo].[Employees] AS [t1] WHERE [t1].[ReportsTo] = [t0].[EmployeeID] )) = @p0 ORDER BY DATEPART(Year, [t0].[HireDate]) DESC, [t0].[FirstName] -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [0]
分页
描述:按照每页10条记录,查询第二页的顾客
查询句法:
var 分页 = (from c in ctx.Customers select c).Skip(10).Take(10);
对应SQL:
SELECT TOP 10 [t1].[CustomerID], [t1].[CompanyName], [t1].[ContactName], [t1].[ContactTitle], [t1].[Address], [t1].[City], [t1].[Region], [t1].[PostalCode], [t1].[Country], [t1].[Phone], [t1].[Fax] FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]) AS [ROW_NUMBER], [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] ) AS [t1] WHERE [t1].[ROW_NUMBER] > @p0 -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [10]
分组
描述:根据顾客的国家分组,查询顾客数大于5的国家名和顾客数
查询句法:
var 一般分组 = from c in ctx.Customers group c by c.Country into g where g.Count() > 5 orderby g.Count() descending select new { 国家 = g.Key, 顾客数 = g.Count() };
对应SQL:
SELECT [t1].[Country], [t1].[value3] AS [顾客数] FROM ( SELECT COUNT(*) AS [value], COUNT(*) AS [value2], COUNT(*) AS [value3], [t0].[Country] FROM [dbo].[Customers] AS [t0] GROUP BY [t0].[Country] ) AS [t1] WHERE [t1].[value] > @p0 ORDER BY [t1].[value2] DESC -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]
描述:根据国家和城市分组,查询顾客覆盖的国家和城市
查询句法:
var 匿名类型分组 = from c in ctx.Customers group c by new { c.City, c.Country } into g orderby g.Key.Country, g.Key.City select new { 国家 = g.Key.Country, 城市 = g.Key.City };
对应SQL:
SELECT [t1].[Country], [t1].[City] FROM ( SELECT [t0].[City], [t0].[Country] FROM [dbo].[Customers] AS [t0] GROUP BY [t0].[City], [t0].[Country] ) AS [t1] ORDER BY [t1].[Country], [t1].[City]
描述:按照是否超重条件分组,分别查询订单数量
查询句法:
var 按照条件分组 = from o in ctx.Orders group o by new { 条件 = o.Freight > 100 } into g select new { 数量 = g.Count(), 是否超重 = g.Key.条件 ? "是" : "否" };
对应SQL:
SELECT (CASE WHEN [t2].[value2] = 1 THEN @p1 ELSE @p2 END) AS [value], [t2].[value] AS [数量] FROM ( SELECT COUNT(*) AS [value], [t1].[value] AS [value2] FROM ( SELECT (CASE WHEN [t0].[Freight] > @p0 THEN 1 WHEN NOT ([t0].[Freight] > @p0) THEN 0 ELSE NULL END) AS [value] FROM [dbo].[Orders] AS [t0] ) AS [t1] GROUP BY [t1].[value] ) AS [t2] -- @p0: Input Currency (Size = 0; Prec = 19; Scale = 4) [100] -- @p1: Input String (Size = 1; Prec = 0; Scale = 0) [是] -- @p2: Input String (Size = 1; Prec = 0; Scale = 0) [否]
distinct
描述:查询顾客覆盖的国家
查询句法:
var 过滤相同项 = (from c in ctx.Customers orderby c.Country select c.Country).Distinct();
对应SQL:
SELECT DISTINCT [t0].[Country] FROM [dbo].[Customers] AS [t0]
union
描述:查询城市是A打头和城市包含A的顾客并按照顾客名字排序
查询句法:
var 连接并且过滤相同项 = (from c in ctx.Customers where c.City.Contains("A") select c).Union (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
对应SQL:
SELECT [t3].[CustomerID], [t3].[CompanyName], [t3].[ContactName], [t3].[ContactTitle], [t3].[Address], [t3].[City], [t3].[Region], [t3].[PostalCode], [t3].[Country], [t3].[Phone], [t3].[Fax] FROM ( SELECT [t2].[CustomerID], [t2].[CompanyName], [t2].[ContactName], [t2].[ContactTitle], [t2].[Address], [t2].[City], [t2].[Region], [t2].[PostalCode], [t2].[Country], [t2].[Phone], [t2].[Fax] FROM ( SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] LIKE @p0 UNION SELECT [t1].[CustomerID], [t1].[CompanyName], [t1].[ContactName], [t1].[ContactTitle], [t1].[Address], [t1].[City], [t1].[Region], [t1].[PostalCode], [t1].[Country], [t1].[Phone], [t1].[Fax] FROM [dbo].[Customers] AS [t1] WHERE [t1].[ContactName] LIKE @p1 ) AS [t2] ) AS [t3] ORDER BY [t3].[ContactName] -- @p0: Input String (Size = 3; Prec = 0; Scale = 0) [%A%] -- @p1: Input String (Size = 2; Prec = 0; Scale = 0) [A%]
concat
描述:查询城市是A打头和城市包含A的顾客并按照顾客名字排序,相同的顾客信息不会过滤
查询句法:
var 连接并且不过滤相同项 = (from c in ctx.Customers where c.City.Contains("A") select c).Concat (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
var 取相交项 = (from c in ctx.Customers where c.City.Contains("A") select c).Intersect (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
对应SQL:
SELECT [t1].[CustomerID], [t1].[CompanyName], [t1].[ContactName], [t1].[ContactTitle], [t1].[Address], [t1].[City], [t1].[Region], [t1].[PostalCode], [t1].[Country], [t1].[Phone], [t1].[Fax] FROM ( SELECT DISTINCT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] ) AS [t1] WHERE (EXISTS( SELECT NULL AS [EMPTY] FROM [dbo].[Customers] AS [t2] WHERE ([t1].[CustomerID] = [t2].[CustomerID]) AND ([t2].[ContactName] LIKE @p0) )) AND ([t1].[City] LIKE @p1) ORDER BY [t1].[ContactName] -- @p0: Input String (Size = 2; Prec = 0; Scale = 0) [A%] -- @p1: Input String (Size = 3; Prec = 0; Scale = 0) [%A%]
排除相交项
描述:查询城市包含A的顾客并从中删除城市以A开头的顾客,并按照顾客名字排序
查询句法:
var 排除相交项 = (from c in ctx.Customers where c.City.Contains("A") select c).Except (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
var 子查询 = from c in ctx.Customers where (from o in ctx.Orders group o by o.CustomerID into o where o.Count() > 5 select o.Key).Contains(c.CustomerID) select c;
对应SQL:
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] WHERE EXISTS( SELECT NULL AS [EMPTY] FROM ( SELECT COUNT(*) AS [value], [t1].[CustomerID] FROM [dbo].[Orders] AS [t1] GROUP BY [t1].[CustomerID] ) AS [t2] WHERE ([t2].[CustomerID] = [t0].[CustomerID]) AND ([t2].[value] > @p0) ) -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]
in操作
描述:查询指定城市中的客户
查询句法:
var in操作 = from c in ctx.Customers where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City) select c;
var innerjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID select p.ProductName;
对应SQL:
SELECT COUNT(*) AS [value] FROM [dbo].[Products] AS [t0] INNER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])
描述:外连接,没有分类的产品也能查询到
查询句法:
var leftjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID into pro from x in pro.DefaultIfEmpty() select p.ProductName;
对应SQL:
SELECT COUNT(*) AS [value] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])
你可能会很奇怪,原先很复杂的SQL使用查询句法会很简单(比如按照条件分组)。但是原先觉得很好理解的SQL使用查询句法会觉得很复杂(比如连接查询)。其实,我们还可以通过其它方式进行连接操作,在以后说DataLoadOptions类型的时候会再说。虽然Linq to sql已经非常智能了,但是对于非常复杂的查询还是建议通过存储过程实现,下次讲解如何调用存储过程。
SET FMTONLY ON; exec Northwind.dbo.sp_singleresultset SET FMTONLY OFF;
这样就可以直接获取存储过程返回的元数据而无须执行存储过程。
其实我们存储过程返回的就是顾客表的数据,如果你觉得为存储过程单独设置结果集实体有些浪费的话可以在存储过程的属性窗口中调整返回类型从“自动生成的类型”到Customer,不过以后你只能通过删除方法面板中的存储过程,然后重新添加来还原到“自动生成的类型”。下面,我们可以写如下的Linq to object代码进行查询:
var 单结果集存储过程 =
from c in ctx.sp_singleresultset() where c.CustomerID.StartsWith("A") select c;
在这里确实是Linq to object的,因为查询句法不会被整句翻译成SQL,而是从存储过程的返回对象中再去对对象进行查询。SQL代码如下:
create proc [dbo].[sp_withparameter] @customerid nchar(5), @rowcount int output as set nocount on set @rowcount = (select count(*) from customers where customerid = @customerid)
create proc [dbo].[sp_withreturnvalue] @customerid nchar(5) as set nocount on if exists (select 1 from customers where customerid = @customerid) return 101 else return 100
IQueryable query = from c in ctx.Customers select c; foreach (Customer c in query) Response.Write(c.CustomerID);
如果你执行两次foreach操作,将会捕获到两次SQL语句的执行:
IQueryable query = from c in ctx.Customers select c; foreach (Customer c in query) Response.Write(c.CustomerID); foreach (Customer c in query) Response.Write(c.ContactName);
对应SQL:
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0]
对于这样的需求,建议你先使用ToList()等方法把查询结果先进行保存,然后再对集合进行查询:
IEnumerable customers = (from c in ctx.Customers select c).ToList(); foreach (Customer c in customers) Response.Write(c.CustomerID); foreach (Customer c in customers) Response.Write(c.ContactName);
延迟执行的优点在于我们可以像拼接SQL那样拼接查询句法,然后再执行:
var query = from c in ctx.Customers select c; var newquery = (from c in query select c).OrderBy(c => c.CustomerID); DataLoadOptions var products = from p in ctx.Products select p; foreach (var p in products) { if (p.UnitPrice > 10) ShowDetail(p.Order_Details); } private void ShowDetail(EntitySet orderdetails) {}
由于ShowDetail方法并没有使用到订单详细信息,所以这个操作只会执行下面的SQL:
SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0] .[Discontinued] FROM [dbo].[Products] AS [t0]
现在修改一下ShowDetail方法:
private void ShowDetail(EntitySet orderdetails) { foreach (var o in orderdetails) { Response.Write(o.Quantity + " "); } }
你会发现Linq to sql对每个价格大于10的产品都根据产品号进行了一次查询:
SELECT [t0].[OrderID], [t0].[ProductID], [t0].[UnitPrice], [t0].[Quantity], [t0].[Discount] FROM [dbo].[Order Details] AS [t0] WHERE [t0].[ProductID] = @p0 -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
DataLoadOptions options = new DataLoadOptions(); options.LoadWith(p => p.Order_Details); ctx.LoadOptions = options; var products = from p in ctx.Products select p; 。。。。。。。。
[t1].[ProductID] AS [ProductID2], [t1].[UnitPrice] AS [UnitPrice2], [t1].[Quantity], [t1].[Discount], ( SELECT COUNT(*) FROM [dbo].[Order Details] AS [t2] WHERE [t2].[ProductID] = [t0].[ProductID] ) AS [count] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON [t1].[ProductID] = [t0].[ProductID] ORDER BY [t0].[ProductID], [t1].[OrderID]
那么,我们怎么限制订单详细表的加载条件那?
DataLoadOptions options = new DataLoadOptions(); options.LoadWith(p => p.Order_Details); options.AssociateWith(p => p.Order_Details.Where(od => od.Quantity > 80)); ctx.LoadOptions = options; var products = from p in ctx.Products select p;
[t1].[ProductID] AS [ProductID2], [t1].[UnitPrice] AS [UnitPrice2], [t1].[Quantity], [t1].[Discount], ( SELECT COUNT(*) FROM [dbo].[Order Details] AS [t2] WHERE ([t2].[Quantity] > @p0) AND ([t2].[ProductID] = [t0].[ProductID]) ) AS [count] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON ([t1].[Quantity] > @p0)
AND ([t1].[ProductID] = [t0].[ProductID]) ORDER BY [t0].[ProductID], [t1].[OrderID] -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [80] DataLoadOptions限制 Linq to sql对DataLoadOptions的使用是有限制的,它只支持1个1对多的关系。一个顾客可能有多个订单,一个订单可能有多个详细订单: DataLoadOptions options = new DataLoadOptions(); options.LoadWith(c => c.Orders); options.LoadWith(o => o.Order_Details); ctx.LoadOptions = options; IEnumerable customers = ctx.Customers.ToList();
[t0].[ShipPostalCode], [t0].[ShipCountry], [t1].[OrderID] AS [OrderID2], [t1].[ProductID], [t1].[UnitPrice],
[t1].[Quantity],
[t1].[Discount], ( SELECT COUNT(*) FROM [dbo].[Order Details] AS [t2] WHERE [t2].[OrderID] = [t0].[OrderID] ) AS [count] FROM [dbo].[Orders] AS [t0] LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON [t1].[OrderID] = [t0].[OrderID] WHERE [t0].[CustomerID] = @x1 ORDER BY [t0].[OrderID], [t1].[ProductID] -- @x1: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) [ALFKI]
[t4].[ShipPostalCode], [t4].[ShipCountry], ( SELECT COUNT(*) FROM [dbo].[Order Details] AS [t5] INNER JOIN [dbo].[Orders] AS [t6] ON [t6].[OrderID] = [t5].[OrderID] WHERE [t5].[ProductID] = [t0].[ProductID] ) AS [count], [t2].[test], [t2].[CategoryID] AS [CategoryID2], [t2].[CategoryName], [t2].[Description], [t2].[Picture] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN ( SELECT 1 AS [test], [t1].[CategoryID], [t1].[CategoryName], [t1].[Description], [t1].[Picture] FROM [dbo].[Categories] AS [t1] ) AS [t2] ON [t2].[CategoryID] = [t0].[CategoryID] LEFT OUTER JOIN ([dbo].[Order Details] AS [t3] INNER JOIN [dbo].[Orders] AS [t4] ON [t4].[OrderID] = [t3].[OrderID]) ON [t3].[ProductID] = [t0].[ProductID] ORDER BY [t0].[ProductID], [t2].[CategoryID], [t3].[OrderID]
主键缓存
Linq to sql对查询过的对象进行缓存,之后的如果只根据主键查询一条记录的话会直接从缓存中读取。比如下面的代码:
var query = from p in ctx.Products where p.CategoryID == 1 select p; foreach (var p in query) p.UnitsInStock = Convert.ToInt16(p.UnitsInStock - 1); ctx.SubmitChanges(); // 在这里设断点
我们使用调试方式启动,由于设置了断点,程序并没有进行更新操作。此时,我们在数据库中运行下面的语句:
update products set unitsinstock = unitsinstock -2, unitprice= unitprice + 1 where categoryid = 1
var query = from p in ctx.Products where p.CategoryID == 1 select p; foreach (var p in query) p.UnitsInStock = Convert.ToInt16(p.UnitsInStock - 1); try { ctx.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException) { foreach (ObjectChangeConflict cc in ctx.ChangeConflicts) { Product p = (Product)cc.Object; Response.Write(p.ProductID + " "); cc.Resolve(RefreshMode.OverwriteCurrentValues); // 放弃当前更新,所有更新以原先更新为准 } } ctx.SubmitChanges();
Linq to sql支持实体的单表继承,也就是基类和派生类都存储在一个表中。对于论坛来说,帖子有两种,一种是主题贴,一种是回复帖。那么,我们就先定义帖子基类:
[Table(Name = "Topics")] public class Topic { [Column(Name = "TopicID", DbType = "int identity", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)] public int TopicID { get; set; } [Column(Name = "TopicType", DbType = "tinyint", CanBeNull = false)] public int TopicType { get; set; } [Column(Name = "TopicTitle", DbType = "varchar(50)", CanBeNull = false)] public string TopicTitle { get; set; } [Column(Name = "TopicContent", DbType = "varchar(max)", CanBeNull = false)] public string TopicContent { get; set; } }
这些实体的定义大家应该很熟悉了。下面,我们再来定义两个实体继承帖子基类,分别是主题贴和回复贴:
public class NewTopic : Topic { public NewTopic() { base.TopicType = 0; } } public class Reply : Topic { public Reply() { base.TopicType = 1; } [Column(Name = "ParentTopic", DbType = "int", CanBeNull = false)] public int ParentTopic { get; set; } }
对于主题贴,在数据库中的TopicType就保存为0,而对于回复贴就保存为1。回复贴还有一个相关字段就是回复所属主题贴的TopicID。那么,我们怎么告知Linq to sql在TopicType为0的时候识别为NewTopic,而1则识别为Reply那?只需稍微修改一下前面的Topic实体定义:
[Table(Name = "Topics")] [InheritanceMapping(Code = 0, Type = typeof(NewTopic), IsDefault = true)] [InheritanceMapping(Code = 1, Type = typeof(Reply))] public class Topic { [Column(Name = "TopicID", DbType = "int identity", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)] public int TopicID { get; set; } [Column(Name = "TopicType", DbType = "tinyint", CanBeNull = false, IsDiscriminator = true)] public int TopicType { get; set; } [Column(Name = "TopicTitle", DbType = "varchar(50)", CanBeNull = false)] public string TopicTitle { get; set; } [Column(Name = "TopicContent", DbType = "varchar(max)", CanBeNull = false)] public string TopicContent { get; set; } }
为类加了InheritanceMapping特性定义,0的时候类型就是NewTopic,1的时候就是Reply。并且为TopicType字段上的特性中加了IsDiscriminator = true,告知Linq to sql这个字段就是用于分类的字段。
实体继承的使用
定义好继承的实体之后,我们就可以使用了。先是自定义一个DataContext吧:
public partial class BBSContext : DataContext { public Table BoardCategories; public Table Boards; public Table Topics; public BBSContext(string connection) : base(connection) { } }
然后,我们来测试一下Linq to sql是否能根据TopicType识别派生类:
BBSContext ctx = new BBSContext("server=xxx;database=BBS;uid=xxx;pwd=xxx"); var query = from t in ctx.Topics select t; foreach (Topic topic in query) { if (topic is NewTopic) { NewTopic newtopic = topic as NewTopic; Response.Write("标题:" + newtopic.TopicTitle + " 类型:" + newtopic.TopicType + " "); }
[Table(Name = "Boards")] public class Board { [Column(Name = "BoardID", DbType = "int identity", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)] public int BoardID { get; set; } [Column(Name = "BoardName", DbType = "varchar(50)", CanBeNull = false)] public string BoardName { get; set; } [Column(Name = "BoardCategory", DbType = "int", CanBeNull = false)] public int BoardCategory { get; set; } private EntityRef _Category; [Association(ThisKey = "BoardCategory", Storage = "_Category")] public BoardCategory Category { get { return this._Category.Entity; } set { this._Category.Entity = value; value.Boards.Add(this); } } }
在这里我们需要关联分类,设置了Category属性使用BoardCategory字段和分类表关联。
实体关系的使用
好了,现在我们就可以在查询句法中直接关联表了(数据库中不一定要设置表的外键关系):
Response.Write("-------------查询分类为1的版块------------- "); var query1 = from b in ctx.Boards where b.Category.CategoryID == 1 select b; foreach (Board b in query1) Response.Write(b.BoardID + " " + b.BoardName + " "); Response.Write("-------------查询版块大于2个的分类------------- "); var query2 = from c in ctx.BoardCategories where c.Boards.Count > 2 select c; foreach (BoardCategory c in query2) Response.Write(c.CategoryID + " " + c.CategoryName + " " + c.Boards.Count + " ");
在数据库中加一些测试数据,如下图:
运行程序后得到下图的结果:
我想定义实体关系的方便我不需要再用语言形容了吧。执行上述的程序会导致下面SQL的执行:
SELECT [t0].[BoardID], [t0].[BoardName], [t0].[BoardCategory] FROM [Boards] AS [t0] INNER JOIN [Categories] AS [t1] ON [t1].[CategoryID] = [t0].[BoardCategory] WHERE [t1].[CategoryID] = @p0 -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1] SELECT [t0].[CategoryID], [t0].[CategoryName] FROM [Categories] AS [t0] WHERE (( SELECT COUNT(*) FROM [Boards] AS [t1] WHERE [t1].[BoardCategory] = [t0].[CategoryID] )) > @p0 -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [2] SELECT [t0].[BoardID], [t0].[BoardName], [t0].[BoardCategory] FROM [Boards] AS [t0] WHERE [t0].[BoardCategory] = @p0 -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
可以看到,第二个查询并没有做外连接,还记得DataLoadOptions吗?我们可以要求Linq to sql在读取版块分类信息的时候也把版块信息一起加载:
DataLoadOptions options = new DataLoadOptions(); options.LoadWith(c => c.Boards); ctx.LoadOptions = options; Response.Write("-------------查询版块大于2个的分类------------- "); var query2 = from c in ctx.BoardCategories where c.Boards.Count > 2 select c; foreach (BoardCategory c in query2) Response.Write(c.CategoryID + " " + c.CategoryName + " " + c.Boards.Count + " ");
查询经过改造后会得到下面的SQL:
SELECT [t0].[CategoryID], [t0].[CategoryName], [t1].[BoardID], [t1].[BoardName], [t1].[BoardCategory], ( SELECT COUNT(*) FROM [Boards] AS [t3] WHERE [t3].[BoardCategory] = [t0].[CategoryID] ) AS [count] FROM [Categories] AS [t0] LEFT OUTER JOIN [Boards] AS [t1] ON [t1].[BoardCategory] = [t0].[CategoryID] WHERE (( SELECT COUNT(*) FROM [Boards] AS [t2] WHERE [t2].[BoardCategory] = [t0].[CategoryID] )) > @p0 ORDER BY [t0].[CategoryID], [t1].[BoardID] -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [2]
var count = (from c in ctx.Customers where c.Region == null select c).Count(); Response.Write(count + " "); var query = from emp in ctx.Employees select emp.ReportsTo; foreach (Nullable r in query) { Response.Write(r.HasValue ? r.Value.ToString() + " " : "没有 "); }
代码执行后捕获到下面的SQL被执行:
SELECT COUNT(*) AS [value] FROM [dbo].[Customers] AS [t0] WHERE [t0].[Region] IS NULL SELECT [t0].[ReportsTo] FROM [dbo].[Employees] AS [t0]
已编译查询
对于一些在项目中经常被用到的查询可以封装成已编译查询,这样就能提高执行效率:
static class Queries { public static Func> CustomersByCity = CompiledQuery.Compile((NorthwindDataContext ctx, string city) =>
from c in ctx. Customers where c.City == city select c);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Contract { [ServiceContract] public interface IDataAccess { [OperationContract] void SendMessage(TbGuestBook gb); [OperationContract] List GetData(); [OperationContract] void DeleteMessage(string ID); [OperationContract] void SendReply(TbGuestBook gb); } }
在这里定义了四个方法:
创建留言
获取所有留言
删除留言
管理员发表回复
然后,我们来实现这个契约,把如下代码保存为DataAccess.cs放在Service类库项目中:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Contract; using System.Data.Linq.Mapping; using System.IO; using System.ServiceModel; namespace Service { [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class DataAccess : IDataAccess { GuestBook ctx; public DataAccess() { XmlMappingSource xms = XmlMappingSource.FromXml(File.ReadAllText("c:\\guestbook.map")); ctx = new GuestBook("server=srv-devdbhost;database=GuestBook;uid=sa;pwd=Abcd1234", xms); ctx.Log = Console.Out; } public void SendMessage(TbGuestBook gb) { ctx.TbGuestBook.Add(gb); ctx.SubmitChanges(); } public List GetData() { var query = from gb in ctx.TbGuestBook orderby gb.PostTime descending select gb; return query.ToList(); } public void DeleteMessage(string ID) { TbGuestBook gb = ctx.TbGuestBook.Single(message => message.ID == new Guid(ID)); ctx.TbGuestBook.Remove(gb); ctx.SubmitChanges(); } public void SendReply(TbGuestBook gb) { //ctx.ExecuteCommand("update tbGuestBook set reply={0},isreplied=1 where ID={1}", gb.Reply, gb.ID); TbGuestBook record = ctx.TbGuestBook.Single(message => message.ID == gb.ID); record.IsReplied = true; record.Reply = gb.Reply; ctx.SubmitChanges(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using Service; using Contract; namespace Host { class Program { static void Main(string[] args) { Uri uri = new Uri("net.tcp://localhost:8080/DataAccessService"); using (ServiceHost sh = new ServiceHost(typeof(DataAccess), uri)) { NetTcpBinding ctb = new NetTcpBinding(); sh.AddServiceEndpoint(typeof(IDataAccess), ctb, string.Empty); sh.Opened += delegate { Console.WriteLine("服务已经启动"); }; sh.Open(); Console.ReadLine(); } } } }
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; using Contract; using System.ServiceModel.Description; using System.ServiceModel; public class GetService { public static IDataAccess GetDataAccessService() { ServiceEndpoint sep = new ServiceEndpoint(ContractDescription.GetContract(typeof(IDataAccess)), new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8080/DataAccessService")); ChannelFactory cf = new ChannelFactory(sep); return cf.CreateChannel(); } }
LINQ to XML 是一种启用了 LINQ 的内存 XML 编程接口,使用它,可以在 .NET Framework 编程语言中处理 XML。
它将 XML 文档置于内存中,这一点很像文档对象模型 (DOM)。 您可以查询和修改 XML 文档,修改之后,可以将其另存为文件,也可以将其序列化然后通过网络发送。 但是,LINQ to XML 与 DOM 不同: 它提供一种新的对象模型,这是一种更轻量的模型,使用也更方便,这种模型利用了 Visual C# 2008 在语言方面的改进。
LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。 由于实现了这一集成,因此,可以对内存 XML 文档编写查询,以检索元素和属性的集合。 LINQ to XML 的查询功能在功能上(尽管不是在语法上)与 XPath 和 XQuery 具有可比性。 Visual C# 2008 集成 LINQ 后,可提供更强的类型化功能、编译时检查和改进的调试器支持。
通过将查询结果用作 XElement 和 XAttribute 对象构造函数的参数,实现了一种功能强大的创建 XML 树的方法。 这种方法称为“函数构造”,利用这种方法,开发人员可以方便地将 XML 树从一种形状转换为另一种形状。
LINQ to XML 提供了改进的 XML 编程接口,这一点可能与 LINQ to XML 的 LINQ 功能同样重要。 通过 LINQ to XML,对 XML 编程时,您可以实现任何预期的操作,包括:
从文件或流加载 XML。
将 XML 序列化为文件或流。
使用函数构造从头开始创建 XML。
使用类似 XPath 的轴查询 XML。
使用 Add、Remove、ReplaceWith 和 SetValue 等方法对内存 XML 树进行操作。
使用 XSD 验证 XML 树。
使用这些功能的组合,可将 XML 树从一种形状转换为另一种形状。
创建 XML 树是否方便,这一点非常重要。 例如,若要创建一个小型 XML 树,可以编写以下 C# 代码:
XElement contacts = new XElement("Contacts", new XElement("Contact", new XElement("Name", "Patrick Hines"), new XElement("Phone", "206-555-0144", new XAttribute("Type", "Home")), new XElement("phone", "425-555-0145", new XAttribute("Type", "Work")), new XElement("Address", new XElement("Street1", "123 Main St"), new XElement("City", "Mercer Island"), new XElement("State", "WA"), new XElement("Postal", "68042") ) ) );
请注意,缩进用于构造 XML 树的代码可显示基础 XML 的结构。
直接使用 XML 元素
在使用 XML 编程时,主要关注的通常是 XML 元素,也可能关注属性。 在 LINQ to XML 中,可以直接使用 XML 元素和属性。 例如,可以执行以下操作:
创建 XML 元素而根本不使用文档对象。 当必须使用 XML 树的片段时,这可简化编程。
直接从 XML 文件加载 T:System.Xml.Linq.XElement 对象。
将 T:System.Xml.Linq.XElement 对象序列化为文件或流。
使用 LINQ to XML 时,仅在文档的根级别添加注释或处理说明时,才需使用 XDocument 类
名称和命名空间的简化处理
处理名称、命名空间和命名空间前缀通常是 XML 编程的复杂部分。LINQ to XML 完全不需要处理命名空间前缀,从而简化了名称和命名空间。 可以轻松控制命名空间前缀。 但如果您决定不显式控制命名空间前缀,则在序列化时,LINQ to XML 将会分配命名空间前缀(如果需要)或使用默认命名空间进行序列化。 如果使用默认命名空间,则生成的文档中将没有命名空间前缀。
LINQ to XML 旨在使 XML 名称尽可能简单。 XML 名称由于复杂而通常被视为 XML 中的高级主题。 有证据证明,这种复杂性不是由开发人员编程时通常使用的命名空间造成的,而是由命名空间前缀造成的。 使用命名空间前缀可以减少输入 XML 时需要的击键数或使 XML 更具可读性。 但前缀通常只是使用完整 XML 命名空间的快捷方式,在多数情况下并不需要。LINQ to XML 通过将所有前缀解析为其对应的 XML 命名空间来简化 XML 名称。 如果需要,可以通过 GetPrefixOfNamespace 方法可以使用前缀。
如果有必要,可以控制命名空间前缀。 在某些情况下,如果使用的是其他 XML 系统(如 XSLT 或 XAML),则需要控制命名空间前缀。 例如,如果 XPath 表达式使用 XSLT 样式表中嵌入的命名空间前缀,则将需要确保使用与 XPath 表达式中使用的前缀相匹配的命名空间前缀来序列化 XML 文档。
XDocument 是从 XContainer 派生的。 因此,它可以包含子节点。 但是,XDocument 对象只能有一个子 XElement 节点。 这反映了 XML 标准,即在 XML 文档中只能有一个根元素。
在没有 Xdocument 的情况下使用 XElement
如上所述,XElement 类是 LINQ to XML 编程接口中的主类。 在很多情况下,您的应用程序不需要您创建文档。 通过使用 XElement 类,可以创建 XML 树,向它添加其他 XML 树,修改 XML 树并进行保存。
若要构造一个 XDocument,可使用函数构造,正如您构造 XElement 对象那样。
下面的代码创建一个 XDocument 对象及其关联的包含对象。
XDocument d = new XDocument( new XComment("This is a comment."), new XProcessingInstruction("xml-stylesheet", "href='mystyle.css' title='Compact' type='text/css'"), new XElement("Pubs", new XElement("Book", new XElement("Title", "Artifacts of Roman Civilization"), new XElement("Author", "Moreno, Jordao") ), new XElement("Book", new XElement("Title", "Midieval Tools and Implements"), new XElement("Author", "Gazit, Inbar") ) ), new XComment("This is another comment.") ); d.Declaration = new XDeclaration("1.0", "utf-8", "true"); Console.WriteLine(d); d.Save("test.xml");
当您检查文件 test.xml 时, 会得到以下输出:
Artifacts of Roman Civilization
Moreno, Jordao
Midieval Tools and Implements
Gazit, Inbar
XElement 类概述
XElement 类是 LINQ to XML 中的基础类之一。 它表示一个 XML 元素。 可以使用该类创建元素;更改元素内容;添加、更改或删除子元素;向元素中添加属性;或以文本格式序列化元素内容。 还可以与 System.Xml 中的其他类(例如 XmlReader、XmlWriter 和 XslCompiledTransform)进行互操作.
XElement 类提供的功能。
构造 XML 树
可以使用各种方式构造 XML 树,包括以下方式:
可以在代码中构造 XML 树。
可以从包括 TextReader、文本文件或 Web 地址 (URL) 在内的各种源解析 XML。
可以使用 XmlReader 来填充树。 有关更多信息,请参见 ReadFrom。
如果您有一个可以将内容写入 XmlWriter 的模块,则可以使用 CreateWriter 方法来创建编写器,将该编写器传递到该模块,然后使用写入 XmlWriter 的内容来填充 XML 树。
但是,创建 XML 树的最常见的方法如下:
XElement contacts = new XElement("Contacts", new XElement("Contact", new XElement("Name", "Patrick Hines"), new XElement("Phone", "206-555-0144"), new XElement("Address", new XElement("Street1", "123 Main St"), new XElement("City", "Mercer Island"), new XElement("State", "WA"), new XElement("Postal", "68042") ) ) );
另一个创建 XML 树的十分常用的方法是使用 LINQ 查询的结果来填充 XML 树,如下面的示例所示:
XElement srcTree = new XElement("Root", new XElement("Element", 1), new XElement("Element", 2), new XElement("Element", 3), new XElement("Element", 4), new XElement("Element", 5) ); XElement xmlTree = new XElement("Root", new XElement("Child", 1), new XElement("Child", 2), from el in srcTree.Elements() where (int)el > 2 select el ); Console.WriteLine(xmlTree);
Represents an XML attribute on a given XML element
XComment
Represents an XML comment
XDeclaration
Represents the opening declaration of an XML document
XDocument
Represents the entirety of an XML document
XElement
Represents a given element within an XML document
XName/XNamespace
Provide a very simple manner to define and reference XML namespaces
二、编程方式创建XML文档
以前的 .NET XML编程模型需要使用很多冗长的 DOM API,而 LINQ to XML 则完全可以用与 DOM 无关的方式与 XML 文档交互。这样不但大大减少了代码行,而且这种编程模型可以直接映射到格式良好的XML文档结构。
static void CreateFunctionalXmlElement() { // A "functional" approach to build an // XML element in memory. XElement inventory = new XElement("Inventory", new XElement("Car", new XAttribute("ID", "1"), new XElement("Color", "Green"), new XElement("Make", "BMW"), new XElement("PetName", "Stan") ) ); // Call ToString() on our XElement. Console.WriteLine(inventory); }
在内存中创建XML文档
static void CreateFunctionalXmlDoc() { XDocument inventoryDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("Current Inventory of AutoLot"), new XElement("Inventory", new XElement("Car", new XAttribute("ID", "1"), new XElement("Color", "Green"), new XElement("Make", "BMW"), new XElement("PetName", "Stan") ), new XElement("Car", new XAttribute("ID", "2"), new XElement("Color", "Pink"), new XElement("Make", "Yugo"), new XElement("PetName", "Melvin") ) ) ); // Display the document and save to disk. Console.WriteLine(inventoryDoc); inventoryDoc.Save("SimpleInventory.xml"); }
三、使用 LINQ 查询创建XML文档
static void CreateXmlDocFromArray() { // Create an anonymous array of anonymous types. var data = new [] { new { PetName = "Melvin", ID = 10 }, new { PetName = "Pat", ID = 11 }, new { PetName = "Danny", ID = 12 }, new { PetName = "Clunker", ID = 13 } }; // Now enumerate over the array to build // an XElement. XElement vehicles = new XElement("Inventory", from c in data select new XElement("Car", new XAttribute("ID", c.ID), new XElement("PetName", c.PetName) ) ); Console.WriteLine(vehicles); }
Ford Yellow 862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS Max
BMW Black Zippy
加载
static void Main(string[] args) { Console.WriteLine("***** Fun with LINQ to XML *****\n"); // Load the Inventory.xml document into memory. XElement doc = XElement.Load("Inventory.xml"); // We will author each of these next PrintAllPetNames(doc); Console.WriteLine(); GetAllFords(doc); Console.ReadLine(); }
遍历
static void PrintAllPetNames(XElement doc) { var petNames = from pn in doc.Descendants("PetName") select pn.Value; foreach (var name in petNames) Console.WriteLine("Name: {0}", name); }
查询
static void GetAllFords(XElement doc) { var fords = from c in doc.Descendants("Make") where c.Value == "Ford" select c; foreach (var f in fords) Console.WriteLine("Name: {0}", f); }
七、修改 XML文档
static void AddNewElements(XElement doc) { // Add 5 new purple Fords to the incoming document. for (int i = 0; i < 5; i++) { // Create a new XElement XElement newCar = new XElement("Car", new XAttribute("ID", i + 1000), new XElement("Color", "Green"), new XElement("Make", "Ford"), new XElement("PetName", "") ); // Add to doc. doc.Add(newCar); } // Show the updates. Console.WriteLine(doc); }
首先引用网络上不知道是谁的一段文字来讲述下什么是linq to xml 如果已经熟悉的就直接跳过这段:
LINQ to XML 是一种启用了 LINQ 的内存 XML 编程接口,使用它,可以在 .NET Framework 编程语言中处理 XML。
它将 XML 文档置于内存中,这一点很像文档对象模型 (DOM)。 您可以查询和修改 XML 文档,修改之后,可以将其另存为文件,也可以将其序列化然后通过网络发送。 但是,LINQ to XML 与 DOM 不同: 它提供一种新的对象模型,这是一种更轻量的模型,使用也更方便,这种模型利用了 Visual C# 2008 在语言方面的改进。
LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。 由于实现了这一集成,因此,可以对内存 XML 文档编写查询,以检索元素和属性的集合。 LINQ to XML 的查询功能在功能上(尽管不是在语法上)与 XPath 和 XQuery 具有可比性。 Visual C# 2008 集成 LINQ 后,可提供更强的类型化功能、编译时检查和改进的调试器支持。
通过将查询结果用作 XElement 和 XAttribute 对象构造函数的参数,实现了一种功能强大的创建 XML 树的方法。 这种方法称为“函数构造”,利用这种方法,开发人员可以方便地将 XML 树从一种形状转换为另一种形状。
LINQ to XML 提供了改进的 XML 编程接口,这一点可能与 LINQ to XML 的 LINQ 功能同样重要。 通过 LINQ to XML,对 XML 编程时,您可以实现任何预期的操作,包括:
· 从文件或流加载 XML。
· 将 XML 序列化为文件或流。
· 使用函数构造从头开始创建 XML。
· 使用类似 XPath 的轴查询 XML。
· 使用 Add、Remove、ReplaceWith 和 SetValue 等方法对内存 XML 树进行操作。
public void createRss() { //首先从数据库里读出所需要的数据 XXXXContext bd = new XXXXContext(); var sq = from p in bd.news orderby p.ID descending select new { p.ID, p.title,p.cont, p.in_date,p.n_type }; //开始创建xml 对于Rss的规范我这里不再啰嗦了,不明白的可以去网上查一下 //下面这一部分为xml里的声明部分,从foreach开始循环item,把sq的数据循环写入xml XElement contacts = new XElement("rss", new XAttribute("version", "2.0"), new XElement("channel", new XElement("title", "半途的Rss"), new XElement("link", "http://www.bantool.cn"), new XElement("description", "C#,linq,vs2008"), new XElement("language", "zh_cn"), new XElement("copyright", "Copyright 2008 bantool"), new XElement("webMaster", "[email protected]"))); //这里开始循环写数据 foreach (var p in sq) { contacts.Element("channel").Add(new XElement("item", new XElement("title", p.title), new XElement("link", "http://www.bantool.cn/news" + p.ID + ".bantool"), new XElement("description", p.cont), new XElement("category", p.n_type), new XElement("pubDate", p.in_date))); } //这里开始操作xml是写入具体的xml文件还是直接输出 //如果是写入文件的话用这个 //contacts.Save("文件路径"); //如果是输出的諙, //先设好输出的类型"text/xml" Response.ContentType = "text/xml"; using (XmlWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8)) //用contacts的WriteTo方法来写 contacts.WriteTo(writer); //这样就OK啦 }
XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. test.html:1
1.什么是NoSQL数据库?NoSQL和RDBMS有什么区别?在哪些情况下使用和不使用NoSQL数据库?
NoSQL是非关系型数据库,NoSQL = Not Only SQL。
关系型数据库采用的结构化的数据,NoSQL采用的是键值对的方式存储数据。
在处理非结构化/半结构化的大数据时;在水平方向上进行扩展时;随时应对动态增加的数据项时可以优先考虑使用NoSQL数据库。
在考虑数据库的成熟
ClientWatchManager接口
//接口的唯一方法materialize用于确定那些Watcher需要被通知
//确定Watcher需要三方面的因素1.事件状态 2.事件类型 3.znode的path
public interface ClientWatchManager {
/**
* Return a set of watchers that should
解决mysql导入导出数据乱码问题方法:
1、进入mysql,通过如下命令查看数据库编码方式:
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------------------+
| Variable_name&nbs
Your love is also your weak point.
你的所爱同时也是你的弱点。
If anything in this life is certain, if history has taught us anything, it is
that you can kill anyone.
不顾家的人永远不可能成为一个真正的男人。 &
用phpMyAdmin导入mysql数据库时,我的10M的
数据库不能导入,提示mysql数据库最大只能导入2M。
phpMyAdmin数据库导入出错: You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.
1、create database school 创建数据库school
2、drop database school 删除数据库school
3、use school 连接到school数据库,使其成为当前数据库
4、create table class(classID int primary key identity not null)
创建一个名为class的表,其有一