一、将多个输入联接到一个输出序列中

可以使用 LINQ 查询创建包含元素的输出序列,这些元素来自多个输入序列。 以下示例演示如何组合两个内存中数据结构,但相同的原则可应用于组合来自 XML 或 SQL 或数据集源的数据。 假设以下两种类类型:

class Student
{
public string First { get; set; }
public string Last {get; set;}
public int ID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public List<int> Scores;
} class Teacher
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public string City { get; set; }
}

以下示例演示了查询:

class DataTransformations
{
static void Main()
{
// Create the first data source.
List<Student> students = new List<Student>()
{
new Student { First="Svetlana",
Last="Omelchenko",
ID=,
Street="123 Main Street",
City="Seattle",
Scores= new List<int> { , , , } },
new Student { First="Claire",
Last="O’Donnell",
ID=,
Street="124 Main Street",
City="Redmond",
Scores= new List<int> { , , , } },
new Student { First="Sven",
Last="Mortensen",
ID=,
Street="125 Main Street",
City="Lake City",
Scores= new List<int> { , , , } },
}; // Create the second data source.
List<Teacher> teachers = new List<Teacher>()
{
new Teacher { First="Ann", Last="Beebe", ID=, City="Seattle" },
new Teacher { First="Alex", Last="Robinson", ID=, City="Redmond" },
new Teacher { First="Michiyo", Last="Sato", ID=, City="Tacoma" }
}; // Create the query.
var peopleInSeattle = (from student in students
where student.City == "Seattle"
select student.Last)
.Concat(from teacher in teachers
where teacher.City == "Seattle"
select teacher.Last); Console.WriteLine("The following students and teachers live in Seattle:");
// Execute the query.
foreach (var person in peopleInSeattle)
{
Console.WriteLine(person);
} Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
The following students and teachers live in Seattle:
Omelchenko
Beebe
*/

二、将内存中对象转换为 XML

LINQ 查询可以方便地在内存中数据结构、SQL 数据库、ADO.NET 数据集和 XML 流或文档之间转换数据。 以下示例将内存中数据结构中的对象转换为 XML 元素。

class XMLTransform
{
static void Main()
{
// Create the data source by using a collection initializer.
// The Student class was defined previously in this topic.
List<Student> students = new List<Student>()
{
new Student {First="Svetlana", Last="Omelchenko", ID=, Scores = new List<int>{, , , }},
new Student {First="Claire", Last="O’Donnell", ID=, Scores = new List<int>{, , , }},
new Student {First="Sven", Last="Mortensen", ID=, Scores = new List<int>{, , , }},
}; // Create the query.
var studentsToXML = new XElement("Root",
from student in students
let scores = string.Join(",", student.Scores)
select new XElement("student",
new XElement("First", student.First),
new XElement("Last", student.Last),
new XElement("Scores", scores)
) // end "student"
); // end "Root" // Execute the query.
Console.WriteLine(studentsToXML); // Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}

此代码生成以下 XML 输出:

<Root>
<student>
<First>Svetlana</First>
<Last>Omelchenko</Last>
<Scores>,,,</Scores>
</student>
<student>
<First>Claire</First>
<Last>O'Donnell</Last>
<Scores>,,,</Scores>
</student>
<student>
<First>Sven</First>
<Last>Mortensen</Last>
<Scores>,,,</Scores>
</student>
</Root>

LINQ-进行数据转换的更多相关文章

  1. C#3.0新增功能09 LINQ 基础05 使用 LINQ 进行数据转换

    连载目录    [已更新最新开发文章,点击查看详细] 语言集成查询 (LINQ) 不只是检索数据. 它也是用于转换数据的强大工具. 通过使用 LINQ查询,可以使用源序列作为输入,并通过多种方式对其进 ...

  2. .NET LINQ数据转换

    使用 LINQ 进行数据转换      语言集成查询 (LINQ) 不仅可用于检索数据, 而且还是一个功能强大的数据转换工具. 通过使用 LINQ 查询,您可以将源序列用作输入,并采用多种方式修改它以 ...

  3. [C#] 走进 LINQ 的世界

    走进 LINQ 的世界 序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串 ...

  4. LINQ 的使用

    [转]链接:cnblogs.com/liqingwen/p/5832322.html LINQ 简介 语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framewo ...

  5. LINQ 操作符

    using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace LinQ ...

  6. C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】

    C#7.2——编写安全高效的C#代码 2018-11-07 18:59 by 沉睡的木木夕, 123 阅读, 0 评论, 收藏, 编辑 原文地址:https://docs.microsoft.com/ ...

  7. Linq基础知识小记三

    1.子查询 Linq中的子查询思想和Sql中的子查询其实差不多, 对于方法语法,一个子查询包含在另一个子查询的Lambda表达式中,代码如下: string[] names = { "Jam ...

  8. LINQ数据库技术

    LINQ(Language Integrated Qyery),中文名字是语言集成查询.它提供一个统一的编程概念和语法,编程人员不需要关心将要访问的是关系数据库还是XML数据,或是远程的对象,它都采用 ...

  9. [转]走进 LINQ 的世界

    序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串> 和 <L ...

  10. C# LINQ学习笔记一:走进LINQ的世界

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5832322.html,记录一下学习过程以备后续查用. LINQ 简介:     语言集成查询(LINQ)是Vi ...

随机推荐

  1. CF1110C Meaningless Operations

    思路: 令x为满足2x <= a的最大的x.如果a的二进制表示中包含0,则将b构造为(2x+1 - 1) ^ a即可:否则gcd(a ^ b, a & b) = gcd(2x+1 - 1 ...

  2. MFC程序添加快捷键

    [问题提出]     有的程序需要自定义组合键完成一定功能,如何实现? [解决方法]     RegisterHotKey函数原型及说明:     BOOL RegisterHotKey(     H ...

  3. easyui 刷新页面

    window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.reload()刷新父窗口对象 ...

  4. "xxadmin" user: No protocol specified 错误

    1 查看DISPLAY是否设置:env| grep DISPLAY 如未设置则,export DISPLAY=192.168.0.9:0.0 (斜体字修改为自己的服务器的ip) 2   root用户执 ...

  5. POJ 3162 Walking Race (树的直径,单调队列)

    题意:给定一棵带边权的n个节点的树,首先要求出每个点的最长路,然后写成序列d[1],d[2]...d[n],然后求满足 区间最大值-区间最小值<=k 的最大区间长度为多少? 思路: 分两步进行: ...

  6. 使用JOSM编辑OpenStreetMap地图

    申明:转载请注明出处! 网上关于JOSM的使用大多只介绍了如何安装和优缺点,对于我这种小白完全还是不会,于是Google了一番,国外关于JOSM的使用的文章还是很多的, 选中一篇讲解的非常详细来翻译, ...

  7. COGS 1913. AC自动机

    ★★   输入文件:ACautomata.in   输出文件:ACautomata.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 对,这就是裸的AC自动机. 要求:在 ...

  8. (五)maven之外置maven

    eclipse外置maven eclipse内置的maven插件是固定版本,如果要用其他版本的maven,可以使用外置maven. ①    在菜单栏上点击“Windows”à“Preferences ...

  9. python 1:列表和字典

    初学Python, 对列表和字典的嵌套使用. phoneBook = [] #列表 list peopleInfo = {} #字典 dict i=0 while i<3: peopleInfo ...

  10. ios 注册功能研究学习

    通常,移动App的注册功能通常采用手机号码注册或者邮箱帐号注册. 不过在国内这样隐私堪忧的环境下,需要手机号来注册会流失不少用户.即便是新浪微博这样的应用,需要绑定手机号也令我不信任.除非是像淘宝.支 ...