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

先声明一个 Student类

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;

    }

//Teacher 类

 class Teacher
    {
        public string first { get; set; }
        public string last { get; set; }
        public int id { get; set; }
        public string city { get; set; }

    }

 

在windows 控制台程序的

static void Main(string[] args)

            List<Student> students = new List<Student>() {
            new  Student{first="Svetlana",last="Omelchenko",id=111,street="123 Main street",
               city="Seattle",scores=new List<int> {75,84,91,39}},
           new  Student{first="Sven",last="Mortensen",id=113,street="125 Main Street",
                city="Lake City",scores=new List<int>{88,94,65,91}
           }
        };
            List<Teacher> teachers = new List<Teacher>() {
            new Teacher {first="Ann", last="Beebe", id=945, city = "Seattle"},
 

            new Teacher {first="Alex", last="Robinson", id=956, city = "Redmond"},


            new Teacher {first="Michiyo", last="Sato", id=972, city = "Tacoma"}

        };
            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:");
            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.


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 studentsToXML = 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(studentsToXML);

 


// Keep the console open in debug mode.


Console.WriteLine(
"Press any key to exit.");


Console.ReadKey();


}

}

 

 

此代码生成下面的 XML 输出:

other

< 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>

 对源元素执行操作

输出序列可能不包含源序列的任何元素或元素属性。
输出可能是通过将源元素用作输入参数计算出的值的序列。
在执行下面这个简单查询时,此查询会输出一个字符串序列,该序列值表示根据 double 类型的元素的源序列进行的计算。

 

注意

如果查询将转换为某个其他域,则不支持在查询表达式中调用方法。
例如,不能在 LINQ to SQL 中调用一般 C# 方法,因为 SQL Server 没有该方法的上下文。
但是,可以将存储过程映射到方法,然后调用方法。

 

 

class FormatQuery

 

{

 


static
void Main()

 


{

 


// 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);

 

 

 


// Keep the console open in debug mode.

 


Console.WriteLine(
"Press any key to exit.");

 


Console.ReadKey();

 


}

 

}

 

/* Output:

 


Area = 3.14

 


Area = 12.56

 


Area = 28.26

 

*/

 

posted on 2011-11-25 10:05  lihg  阅读(245)  评论(0)    收藏  举报