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

可以使用 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. linux学习笔记汇总

    linux 文件系统是采用级层树状的目录结构,采用"/"根目录的方式 目录结构: / 根目录           |---root: 存放root用户相关的文件          ...

  2. Android ScrollView嵌套RecyclerView导致在三星s8曲面屏显示不全问题

    当RecyclerView适配显示不全时可以单独给其嵌套一个相对布局!!!(必须是相对布局),这样在曲面屏手机就可以全部显示出来如下图所示 <RelativeLayout android:lay ...

  3. 最详细的github快速入门教程

    一:下载github 二:安装GitHub 下载之后点击 进行安装过程,安装之后桌面上会有两个图标,如下图 三:新建项目 GitHub是图形界面模式,Git Shell是命令行模式,在Windows系 ...

  4. siege4安装和使用介绍

    使用文档参考地址:https://www.joedog.org/siege-manual/ siege4地址:http://download.joedog.org/siege/ cd /usr/loc ...

  5. vba 时间

    Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...

  6. select *from where 和select *from jion on 语句的差别

    https://zhidao.baidu.com/question/541791438.html select 学号 a,成绩 a,姓名 b from 成绩表 a,学生表 b where a.学号=b ...

  7. Matplotlib_常用图表

    Matplotlib绘图一般用于数据可视化 1.常用的图表有: 折线图(坐标系图) 散点图/气泡图 条形图/柱状图 饼图 直方图 箱线图 热力图 折线图(坐标系图) 折线图用于显示随时间或有序类别的变 ...

  8. Python基础篇 -- if while 语句

    2.7 if语句 # 单纯if if 条件: 代码块 当条件成立,执行代码块 # 二选一 if 条件: 代码块1 else: 代码块2 #当条件为真,执行代码块1,否则执行代码块2 # 多选一 没有e ...

  9. (1)JSTL的13个core标签库

     标准标签库JSTL的全名为:Java Server Pages Standard Tag Library. (jsp  standard tag library) JSTL主要提供了5大类标签库: ...

  10. tp5 -- 腾讯云cos简单使用

    因项目需要,本来是需要对接阿里云oss,但因客户错误将云存储买成腾讯云cos,因此简单做了个对象上传使用 首先下载cos的sdk: 三种方式在文档上面都有介绍 SDK 安装有三种方式:Composer ...