<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
            C語言

            c#關鍵字查詢之select 子句運用

            時間:2025-02-07 05:01:31 C語言 我要投稿
            • 相關推薦

            c#關鍵字查詢之select 子句運用

            引導語:在計算機中,select語句是最常用的數據查詢語句。以下是小編整理的c#關鍵字查詢之select 子句運用,歡迎參考閱讀!

              在查詢表達式中,select 子句可以指定將在執行查詢時產生的值的類型。該子句的結果將基于前面所有子句的計算結果以及 select 子句本身中的所有表達式。查詢表達式必須以 select 子句或 group 子句結束。

              下面的示例演示了查詢表達式中的簡單 select 子句。

              C#  

              class SelectSample1

              {

              static void Main()

              {

              //Create the data source

              List<int> Scores = new List<int>() { 97, 92, 81, 60 };

              // Create the query.

              IEnumerable<int> queryHighScores =

              from score in Scores

              where score > 80

              select score;

              // Execute the query.

              foreach (int i in queryHighScores)

              {

              Console.Write(i + " ");

              }

              }

              }

              //Output: 97 92 81

              select 子句產生的序列的類型決定了查詢變量 queryHighScores 的類型。在最簡單的情況下,select 子句僅指定范圍變量。這會使返回的序列包含與數據源具有相同類型的元素。有關更多信息,請參見查詢操作中的類型關系 (LINQ)。不過,select 子句還提供了一種功能強大的機制,可用于將源數據轉換(或投影)為新類型。有關更多信息,請參見使用 LINQ 進行數據轉換。

              示例

              下面的示例演示了 select 子句可能采用的所有不同形式。在每個查詢中,請注意 select 子句和查詢變量(studentQuery1、studentQuery2 等)的類型之間的關系。

              C#  

              class SelectSample2

              {

              // Define some classes

              public class Student

              {

              public string First { get; set; }

              public string Last { get; set; }

              public int ID { get; set; }

              public List<int> Scores;

              public ContactInfo GetContactInfo(SelectSample2 app, int id)

              {

              ContactInfo cInfo =

              (from ci in app.contactList

              where ci.ID == id

              select ci)

              .FirstOrDefault();

              return cInfo;

              }

              public override string ToString()

              {

              return First + " " + Last + ":" + ID;

              }

              }

              public class ContactInfo

              {

              public int ID { get; set; }

              public string Email { get; set; }

              public string Phone { get; set; }

              public override string ToString() { return Email + "," + Phone; }

              }

              public class ScoreInfo

              {

              public double Average { get; set; }

              public int ID { get; set; }

              }

              // The primary 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}},

              new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int>() {97, 89, 85, 82}},

              };

              // Separate data source for contact info.

              List<ContactInfo> contactList = new List<ContactInfo>()

              {

              new ContactInfo {ID=111, Email="SvetlanO@Contoso.com", Phone="206-555-0108"},

              new ContactInfo {ID=112, Email="ClaireO@Contoso.com", Phone="206-555-0298"},

              new ContactInfo {ID=113, Email="SvenMort@Contoso.com", Phone="206-555-1130"},

              new ContactInfo {ID=114, Email="CesarGar@Contoso.com", Phone="206-555-0521"}

              };

              static void Main(string[] args)

              {

              SelectSample2 app = new SelectSample2();

              // Produce a filtered sequence of unmodified Students.

              IEnumerable<Student> studentQuery1 =

              from student in app.students

              where student.ID > 111

              select student;

              Console.WriteLine("Query1: select range_variable");

              foreach (Student s in studentQuery1)

              {

              Console.WriteLine(s.ToString());

              }

              // Produce a filtered sequence of elements that contain

              // only one property of each Student.

              IEnumerable<String> studentQuery2 =

              from student in app.students

              where student.ID > 111

              select student.Last;

              Console.WriteLine(" studentQuery2: select range_variable.Property");

              foreach (string s in studentQuery2)

              {

              Console.WriteLine(s);

              }

              // Produce a filtered sequence of objects created by

              // a method call on each Student.

              IEnumerable<ContactInfo> studentQuery3 =

              from student in app.students

              where student.ID > 111

              select student.GetContactInfo(app, student.ID);

              Console.WriteLine(" studentQuery3: select range_variable.Method");

              foreach (ContactInfo ci in studentQuery3)

              {

              Console.WriteLine(ci.ToString());

              }

              // Produce a filtered sequence of ints from

              // the internal array inside each Student.

              IEnumerable<int> studentQuery4 =

              from student in app.students

              where student.ID > 111

              select student.Scores[0];

              Console.WriteLine(" studentQuery4: select range_variable[index]");

              foreach (int i in studentQuery4)

              {

              Console.WriteLine("First score = {0}", i);

              }

              // Produce a filtered sequence of doubles

              // that are the result of an expression.

              IEnumerable<double> studentQuery5 =

              from student in app.students

              where student.ID > 111

              select student.Scores[0] * 1.1;

              Console.WriteLine(" studentQuery5: select expression");

              foreach (double d in studentQuery5)

              {

              Console.WriteLine("Adjusted first score = {0}", d);

              }

              // Produce a filtered sequence of doubles that are

              // the result of a method call.

              IEnumerable<double> studentQuery6 =

              from student in app.students

              where student.ID > 111

              select student.Scores.Average();

              Console.WriteLine(" studentQuery6: select expression2");

              foreach (double d in studentQuery6)

              {

              Console.WriteLine("Average = {0}", d);

              }

              // Produce a filtered sequence of anonymous types

              // that contain only two properties from each Student.

              var studentQuery7 =

              from student in app.students

              where student.ID > 111

              select new { student.First, student.Last };

              Console.WriteLine(" studentQuery7: select new anonymous type");

              foreach (var item in studentQuery7)

              {

              Console.WriteLine("{0}, {1}", item.Last, item.First);

              }

              // Produce a filtered sequence of named objects that contain

              // a method return value and a property from each Student.

              // Use named types if you need to pass the query variable

              // across a method boundary.

              IEnumerable<ScoreInfo> studentQuery8 =

              from student in app.students

              where student.ID > 111

              select new ScoreInfo

              {

              Average = student.Scores.Average(),

              ID = student.ID

              };

              Console.WriteLine(" studentQuery8: select new named type");

              foreach (ScoreInfo si in studentQuery8)

              {

              Console.WriteLine("ID = {0}, Average = {1}", si.ID, si.Average);

              }

              // Produce a filtered sequence of students who appear on a contact list

              // and whose average is greater than 85.

              IEnumerable<ContactInfo> studentQuery9 =

              from student in app.students

              where student.Scores.Average() > 85

              join ci in app.contactList on student.ID equals ci.ID

              select ci;

              Console.WriteLine(" studentQuery9: select result of join clause");

              foreach (ContactInfo ci in studentQuery9)

              {

              Console.WriteLine("ID = {0}, Email = {1}", ci.ID, ci.Email);

              }

              // Keep the console window open in debug mode

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

              Console.ReadKey();

              }

              }

              /* Output

              Query1: select range_variable

              Claire O'Donnell:112

              Sven Mortensen:113

              Cesar Garcia:114

              studentQuery2: select range_variable.Property

              O'Donnell

              Mortensen

              Garcia

              studentQuery3: select range_variable.Method

              ClaireO@Contoso.com,206-555-0298

              SvenMort@Contoso.com,206-555-1130

              CesarGar@Contoso.com,206-555-0521

              studentQuery4: select range_variable[index]

              First score = 75

              First score = 88

              First score = 97

              studentQuery5: select expression

              Adjusted first score = 82.5

              Adjusted first score = 96.8

              Adjusted first score = 106.7

              studentQuery6: select expression2

              Average = 72.25

              Average = 84.5

              Average = 88.25

              studentQuery7: select new anonymous type

              O'Donnell, Claire

              Mortensen, Sven

              Garcia, Cesar

              studentQuery8: select new named type

              ID = 112, Average = 72.25

              ID = 113, Average = 84.5

              ID = 114, Average = 88.25

              studentQuery9: select result of join clause

              ID = 114, Email = CesarGar@Contoso.com

              */

              如上一個示例中的 studentQuery8 所示,您有時可能希望所返回序列中的元素僅包含源元素的屬性子集。通過使返回的序列盡可能地小一些,可以降低內存需求,并提高查詢的執行速度。通過在 select 子句中創建一個匿名類型,并且借助于對象初始值設定項用源元素中的適當屬性對該匿名類型進行初始化,可以達到此目的。


            【c#關鍵字查詢之select 子句運用】相關文章:

            c#查詢關鍵字之join 子句運用方法07-01

            c#查詢關鍵字where 子句的運用10-31

            c#查詢關鍵字之group子句的使用09-07

            c#查詢關鍵字from 子句的用法11-07

            c#查詢關鍵字之into的使用07-25

            c#運算符關鍵字is的使用10-30

            C#排序算法之快速排序09-09

            C#排序算法之堆排序07-21

            PHP中模糊查詢并關聯三個select框的操作方法10-02

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院