LINQ-进行数据转换
一、将多个输入联接到一个输出序列中
可以使用 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-进行数据转换的更多相关文章
- C#3.0新增功能09 LINQ 基础05 使用 LINQ 进行数据转换
连载目录 [已更新最新开发文章,点击查看详细] 语言集成查询 (LINQ) 不只是检索数据. 它也是用于转换数据的强大工具. 通过使用 LINQ查询,可以使用源序列作为输入,并通过多种方式对其进 ...
- .NET LINQ数据转换
使用 LINQ 进行数据转换 语言集成查询 (LINQ) 不仅可用于检索数据, 而且还是一个功能强大的数据转换工具. 通过使用 LINQ 查询,您可以将源序列用作输入,并采用多种方式修改它以 ...
- [C#] 走进 LINQ 的世界
走进 LINQ 的世界 序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串 ...
- LINQ 的使用
[转]链接:cnblogs.com/liqingwen/p/5832322.html LINQ 简介 语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framewo ...
- LINQ 操作符
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace LinQ ...
- 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/ ...
- Linq基础知识小记三
1.子查询 Linq中的子查询思想和Sql中的子查询其实差不多, 对于方法语法,一个子查询包含在另一个子查询的Lambda表达式中,代码如下: string[] names = { "Jam ...
- LINQ数据库技术
LINQ(Language Integrated Qyery),中文名字是语言集成查询.它提供一个统一的编程概念和语法,编程人员不需要关心将要访问的是关系数据库还是XML数据,或是远程的对象,它都采用 ...
- [转]走进 LINQ 的世界
序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串> 和 <L ...
- C# LINQ学习笔记一:走进LINQ的世界
本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5832322.html,记录一下学习过程以备后续查用. LINQ 简介: 语言集成查询(LINQ)是Vi ...
随机推荐
- 访问者模式和php实现
访问者模式: 表示作用于某个对象结构中的各个元素的操作.它使你可以在不改变各个元素类的前提下定义作用于这些元素的操作. 角色: 1)抽象访问者:为该对象结构中具体元素角色声明一个访问操作接口.该操作接 ...
- SQL 时间日期函数
1.获取当前日期GetDate getdate()函数以datetime数据类型的格式返回当前SQLServer服务器所在计算机的日期和时间.其语法格式为getdate().返回值舍入到最近的秒小数部 ...
- win10搭建Java环境
一.下载地址 jdk和jre官方网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 根据你的系统选择你需要 ...
- input提示文字;placeholder字体修改
在很多网站上我们都看到input输入框显示提示文字,让我们一起来看看如果在input输入框中显示提示文字.我们只需要在<input>标签里添加:placeholder="提示文字 ...
- Catalan 数
概要 在一些面试的智力题中会遇到此数的变形,如果完全不了解,直接想结果是很困难的,故在此简单介绍一下. 基本定义 Catalan 数的定义根据不同的应用环境有很多不同的定义方式,下面给出一个. ...
- 一个小笔记(5):A*算法
A-Star算法是一种静态路网中求解最短路径最有效的直接搜索方法其实百科有 http://baike.baidu.com/link?url=CvmkWQIAmztYgMq3Nk1WyWkDiC0koV ...
- Codeforces Round #271 (Div. 2)-B. Worms
http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...
- tomcat BIO 、NIO 、AIO
11.11活动当天,服务器负载过大,导致部分页面出现了不可访问的状态.那后来主管就要求调优了,下面是tomcat bio.nio.apr模式以及后来自己测试的一些性能结果. 原理方面的资料都是从网上找 ...
- struts2的动态方法配置
动态方法调用配置 <package name="test" extends="struts-default"> <aciton name=&q ...
- Bootstrap select(选择列表)
当您想让用户从多个选项中进行选择,但是默认情况下只能选择一个选项,则使用选择框 1.使用<select>展示列表选项 2.使用multiple="multiple"允许 ...