Data Transformations with LINQ(第三章使用linq进行数据转换(加工))

LINQ不仅仅可以用来获得数据。他也是一个强大的数据转换加工工具。使用linq可以对数据进行排行,分组等。可以把多个最初的结果合并成一个结果输出。也可以吧一个结果分成多个部分(比如吧一个表中的部分字段进行输出)。还可以吧结果转换为xml等等

 一排序和分组

排序可能是大家常用的最简单的数据操作方法了。在linq中可以使用orderby从句才执行排序。这没什么好说的。大家看看列子就知道了。和sqlserver排序没什么区别(cust是一个表。name是他的字段。是字符类型)

var query = from cust in Customer
            orderby cust.Name
            select cust;
还有个分组
var query = from cust in Customer
            group cust by cust.Name
这些东西和sqlserver是一致的,如果不明白还是看sqlserver的帮助比较好些。
二。将多个输入数据组合成一个输出结果
(翻译可能不准,其实就是sql中join的应用的。将多个表通过join on连链接然后输出)
先建立两个类(我认为就是模拟两个数据表)
class Address
    {
        public int ID {get;set;}
        public string Street {get;set;}
        public string City {get;set;}
    }
class Student
{
    public string First { get; set; }
    public string Last {get; set;}
    public int ID { get; set; }
    public List<int> Scores;
}
然后是填充数据。也就是创建数据源
// Create the first data source
List<Address> addresses = new List<Address>()
{
    new Address {ID=111, Street="123 Main Street", City="Seattle"},
    new Address {ID=112, Street="124 Main Street", City="Redmond"},
    new Address {ID=113, Street="125 Main Street", City="Lake City"},
    new Address {ID=114, Street="126 Main Street", City="North Bend"},
    new Address {ID=115, Street="127 Main Street", City="Issaquah"},
    new Address {ID=116, Street="129 Main Street", City="Tacoma"}
};

// Create the second data source
List<Student> students = new List<Student>()
{
    new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
    new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
    new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
};
开始创建查询
// Create the query.
var query = from student in students
            join address in addresses on student.ID equals address.ID
            select new { student.Last, student.First, address.City };
执行查询
// Execute the query.
foreach (var s in query)
    Console.WriteLine(s.First + " " + s.Last + ", " + s.City);  
三返回结果数据的子集或元素
(类似返回一个表的某个字段)
第一个方法就是返回结果的对象的一个字段或元素
var query = from cust in Customers
            select cust.City;
第二个方法创建一个对象包含要返回的一个或多个字段对象或元素。(其实后面是一个表达式而已)
var query = from cust in Customer
            select new {Name = cust.Name, City = cust.City};

四吧内存中的数据转换为xml(Transforming in-Memory Objects into XML)

linq可以把内存中数据。数据库。ado.net,xml流和文档。转换为xml

// Create the data source.
List<Student> students = new List<Student>()
{
    new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores = new List<int>{97, 92, 81, 60}},
    new Student {First="Claire", Last="O’Donnell", ID=112, Scores = new List<int>{75, 84, 91, 39}},
    new Student {First="Sven", Last="Mortensen", ID=113, Scores = new List<int>{88, 94, 65, 91}},
};

// Create the query.
var query = new XElement("Root",
    from student in students
    let x = String.Format("{0},{1},{2},{3}", student.Scores[0],
            student.Scores[1], student.Scores[2], student.Scores[3])
    select new XElement("student",
               new XElement("First", student.First),
               new XElement("Last", student.Last),
               new XElement("Scores", x)
            ) // end "student"
        ); // end "Root"

// Execute the query.
Console.WriteLine(query);

结果:

< Root>
  <student>
    <First>Svetlana</First>
    <Last>Omelchenko</Last>
    <Scores>97,92,81,60</Scores>
  </student>
  <student>
    <First>Claire</First>
    <Last>O'Donnell</Last>
    <Scores>75,84,91,39</Scores>
  </student>
  <student>
    <First>Sven</First>
    <Last>Mortensen</Last>
    <Scores>88,94,65,91</Scores>
  </student>
</Root>
五:Performing Operations on Source Elements(对源对象进行操作)
// Data source.
double[] radii = { 1, 2, 3 };

// Query.
IEnumerable<string> query =
    from rad in radii
    select String.Format("Area = {0}", (rad * rad) * 3.14);

// Query execution. 
foreach (string s in query)
    Console.WriteLine(s);
这个列子的意思我认为就是可以直接对源数据结果进行操作。是在他们返回结果出来之前进行的。
 

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

你可能感兴趣的:(transform)