关于LINQ XML from SQL
一个从数据库读取数据转换为XML的程序,输出的XML中 属性ID排最后 但是我select语句 要求的ID是第一输出 这个程序 哪里把输出倒序了?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace XMLfromSQL
{
class Program
{
static void Main(string[] args)
{
DataClasses1DataContext dataClass = new DataClasses1DataContext();
//问题:输出的数据是倒序 ID 为最后。 为什么?
XElement XEdataClass =
new XElement("customers",
from c in dataClass.Customers.AsEnumerable() //linq延迟查询 ,用AsEnumerable把结果转化为内存中的linqtoObject
select new XElement("customer",
new XAttribute("ID", c.CustomerID),
new XAttribute("City", c.City),
new XAttribute("Company", c.CompanyName),
from o in c.Orders
select new XElement("order",
new XAttribute("orderId", o.CustomerID),
new XAttribute("orderDay", o.OrderDate.Value.Day),
new XAttribute("orderMonth", o.OrderDate.Value.Month),
new XAttribute("orderYear", o.OrderDate.Value.Year),
new XAttribute("orderTotal", o.Order_Details.Sum(od => od.Quantity * od.UnitPrice))
)//end order
)//end customer
);//end customers
string xmlFileName = @"D:\C\C#实验室\Chapter24\XMLfromSQL\XMLfromSQL.xml";
XEdataClass.Save(xmlFileName);
Console.WriteLine(xmlFileName);
Console.ReadKey();
}
}
}
C#
linq
xml
数据库
--------------------编程问答--------------------
补充:.NET技术 , C#