Linq to sql 结合Linq to xml

本文介绍如何使用C#和Visual Basic中的LINQ来查询和生成XML文件的方法。通过两个示例展示了从数据库中获取数据并将其转换为XML格式的过程。

//C#的例子

 

using System;
using System.Collections;
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;

namespace linq_part7_csharp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            //var q = from p in db.Products
            //        select new
            //        {
            //            Name = db.MyUpperFunction(p.ProductName)
            //        };

            //GridView1.DataSource = q;
            //GridView1.DataBind();

            XElement xml = new XElement("Products",
                                from c in db.Products
                                orderby c.ProductName
                                select new XElement("Produ",
                                          new XAttribute("id", c.ProductID),
                                          new XElement("产品名称", c.ProductName),
                                          new XElement("价格", c.UnitPrice))
                                );

           
            xml.Save(@"C:\xml.xml");
            System.Diagnostics.Process.Start(@"C:\xml.xml");


            // Loading from a file, you can also load from a stream
            //XDocument loaded = XDocument.Load(@"C:\xml.xml");
            //var q = from c in loaded.Descendants("contact")
            //        where (int)c.Attribute("contactId") < 4 
            //        select (string)c.Element("firstName") + "" +
            //           (string)c.Element("lastName");


            //        foreach (string name in q)
            //            Console.WriteLine("Customer name = {0}", name);

        }
    }
}

 

//vb的例子,感觉更清晰和直观

 

Module Module1

    Sub Main()

        Dim db As New NorthwindDataContext

        'Dim customers = <customers>
        '                    <%= From customer In db.Customers _
        '                        Select <customer>
        '                                   <name><%= customer.CompanyName %></name>
        '                                   <phone><%= customer.Phone %></phone>
        '                                   <country><%= customer.Country %></country>
        '                               </customer> %>
        '                </customers>

 

        'Dim customers = <html>
        '                    <body>
        '                        <table width="100%" border="0">
        '                            <tr>
        '                                <td><b>Name</b></td>
        '                                <td><b>Phone</b></td>
        '                                <td><b>Country</b></td>
        '                            </tr>
        '                            <%= From customer In db.Customers _
        '                                Select <tr>
        '                                           <td><%= customer.CompanyName %></td>
        '                                           <td><%= customer.Phone %></td>
        '                                           <td><%= customer.Country %></td>
        '                                       </tr> %>
        '                        </table>
        '                    </body>
        '                </html>

        'customers.Save("c:\customers.html")
        'Process.Start("C:\customers.html")


        Dim orders = <orders>
                         <%= From customer In db.Customers _
                             Select <customer>
                                        <name><%= customer.CompanyName %></name>
                                        <phone><%= customer.Phone %></phone>
                                        <orders>
                                            <%= From order In customer.Orders _
                                                Select <order total=<%= Aggregate detail In order.Order_Details Into Sum(detail.UnitPrice * detail.Quantity) %>>
                                                           <date><%= order.OrderDate %></date>
                                                           <address><%= order.ShipAddress %></address>
                                                           <details>
                                                               <%= From detail In order.Order_Details _
                                                                   Select <item id=<%= detail.ProductID %>>
                                                                              <product><%= detail.Product.ProductName %></product>
                                                                          </item> %>
                                                           </details>
                                                       </order> %>
                                        </orders>
                                    </customer> %>
                     </orders>

        orders.Save("C:\orders.xml")
        Process.Start("C:\orders.xml")

 

    End Sub

End Module

源码来自:https://pan.quark.cn/s/7a757c0c80ca 《在Neovim中运用Lua的详尽教程》在当代文本编辑器领域,Neovim凭借其卓越的性能、可扩展性以及高度可定制的特点,赢得了程序开发者的广泛青睐。 其中,Lua语言的融入更是为Neovim注入了强大的活力。 本指南将深入剖析如何在Neovim中高效地运用Lua进行配置和插件开发,助你充分发挥这一先进功能的潜力。 一、Lua为何成为Neovim的优选方案经典的Vim脚本语言(Vimscript)虽然功能完备,但其语法结构现代化编程语言相比显得较为复杂。 此形成对比的是,Lua是一种精简、轻量且性能卓越的脚本语言,具备易于掌握、易于集成的特点。 因此,Neovim选择Lua作为其核心扩展语言,使得配置和插件开发过程变得更加直观和便捷。 二、安装设置在Neovim中启用Lua支持通常十分简便,因为Lua是Neovim的固有组件。 然而,为了获得最佳体验,我们建议升级至Neovim的最新版本。 可以通过`vim-plug`或`dein.vim`等包管理工具来安装和管理Lua插件。 三、Lua基础在着手编写Neovim的Lua配置之前,需要对Lua语言的基础语法有所掌握。 Lua支持变量、函数、控制流、表(类似于数组和键值对映射)等核心概念。 它的语法设计简洁明了,便于理解和应用。 例如,定义一个变量并赋值:```lualocal myVariable = "Hello, Neovim!"```四、Lua在Neovim中的实际应用1. 配置文件:Neovim的初始化文件`.vimrc`能够完全采用Lua语言编写,只需在文件首部声明`set runtimepath^=~/.config/nvim ini...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值