程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .NET LINQ數據轉換

.NET LINQ數據轉換

編輯:C#入門知識

  • 使用 LINQ 進行數據轉換
  • 將多個輸入聯接到一個輸出序列
 Student
{
      First { ; ; }
      Last {; ;}
      ID { ; ; }
      Street { ; ; }
      City { ; ; }
     List<> Scores;
}
 Teacher
{
      First { ; ; }
      Last { ; ; }
      ID { ; ; } 
      City { ; ; }
}
 DataTransformations
{
      Main()
    {
        List<Student> students =  List<Student>()
        {
             Student {First=,
                Last=, 
                ID=111, 
                Street=,
                City=,
                Scores=  List<> {97, 92, 81, 60}},
             Student {First=,
                Last=, 
                ID=112,
                Street=,
                City=,
                Scores=  List<> {75, 84, 91, 39}},
             Student {First=,
                Last=,
                ID=113,
                Street=,
                City=,
                Scores=  List<> {88, 94, 65, 91}},
        };
        List<Teacher> teachers =  List<Teacher>()
        {                
             Teacher {First=, Last=, ID=945, City = },
             Teacher {First=, Last=, ID=956, City = },
             Teacher {First=, Last=, ID=972, City = }
        };
         peopleInSeattle = ( student  students
                     student.City == 
                     student.Last)
                    .Concat( teacher  teachers
                             teacher.City == 
                             teacher.Last);

        Console.WriteLine();
         ( person  peopleInSeattle)
        {
            Console.WriteLine(person);
        }
        Console.WriteLine();
        Console.ReadKey();
    }
}
  • 選擇各個源元素的子集

















  • 將內存中的對象轉換為 XML
 XMLTransform
{
      Main()
    {            
        List<Student> students =  List<Student>()
        {
             Student {First=, Last=, ID=111, Scores =  List<>{97, 92, 81, 60}},
             Student {First=, Last=, ID=112, Scores =  List<>{75, 84, 91, 39}},
             Student {First=, Last=, ID=113, Scores =  List<>{88, 94, 65, 91}},
        };
         studentsToXML =  XElement(,
             student  students
             x = String.Format(, student.Scores[0],
                    student.Scores[1], student.Scores[2], student.Scores[3])
              XElement(,
                        XElement(, student.First),
                        XElement(, student.Last),
                        XElement(, x)
                    ) 
                ); 
        Console.WriteLine(studentsToXML);
        Console.WriteLine();
        Console.ReadKey();
    }
}
  • 對源元素執行操作
 FormatQuery
{
      Main()
    {            
        [] radii = { 1, 2, 3 };
        IEnumerable<> query =
             rad  radii
             String.Format(, (rad * rad) * 3.14);
         ( s  query)
            Console.WriteLine(s);
        Console.WriteLine();
        Console.ReadKey();
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved