网站首页 语言 会计 互联网计算机 医学 学历 职场 文艺体育 范文
当前位置:学识谷 > 计算机 > C语言

c#关键字查询之select 子句运用

栏目: C语言 / 发布于: / 人气:2.71W

引导语:在计算机中,select语句是最常用的数据查询语句。以下是小编整理的c#关键字查询之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)

{

e(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 actList

where == id

select ci)

tOrDefault();

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, ", Phone="206-555-0108"},

new ContactInfo {ID=112, ", Phone="206-555-0298"},

new ContactInfo {ID=113, ", Phone="206-555-1130"},

new ContactInfo {ID=114, ", 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 ents

where > 111

select student;

eLine("Query1: select range_variable");

foreach (Student s in studentQuery1)

{

eLine(ring());

}

// Produce a filtered sequence of elements that contain

// only one property of each Student.

IEnumerable<String> studentQuery2 =

from student in ents

where > 111

select ;

eLine(" studentQuery2: select range_erty");

foreach (string s in studentQuery2)

{

eLine(s);

}

// Produce a filtered sequence of objects created by

// a method call on each Student.

IEnumerable<ContactInfo> studentQuery3 =

from student in ents

where > 111

select ontactInfo(app, );

eLine(" studentQuery3: select range_od");

foreach (ContactInfo ci in studentQuery3)

{

eLine(ring());

}

// Produce a filtered sequence of ints from

// the internal array inside each Student.

IEnumerable<int> studentQuery4 =

from student in ents

where > 111

select es[0];

eLine(" studentQuery4: select range_variable[index]");

foreach (int i in studentQuery4)

{

eLine("First score = {0}", i);

}

// Produce a filtered sequence of doubles

// that are the result of an expression.

IEnumerable<double> studentQuery5 =

from student in ents

where > 111

select es[0] * 1.1;

eLine(" studentQuery5: select expression");

foreach (double d in studentQuery5)

{

eLine("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 ents

where > 111

select age();

eLine(" studentQuery6: select expression2");

foreach (double d in studentQuery6)

{

eLine("Average = {0}", d);

}

// Produce a filtered sequence of anonymous types

// that contain only two properties from each Student.

var studentQuery7 =

from student in ents

where > 111

select new { t, };

eLine(" studentQuery7: select new anonymous type");

foreach (var item in studentQuery7)

{

eLine("{0}, {1}", , t);

}

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

where > 111

select new ScoreInfo

{

Average = age(),

ID =

};

eLine(" studentQuery8: select new named type");

foreach (ScoreInfo si in studentQuery8)

{

eLine("ID = {0}, Average = {1}", , age);

}

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

where age() > 85

join ci in actList on equals

select ci;

eLine(" studentQuery9: select result of join clause");

foreach (ContactInfo ci in studentQuery9)

{

eLine("ID = {0}, Email = {1}", , l);

}

// Keep the console window open in debug mode

eLine("Press any key to exit.");

Key();

}

}

/* Output

Query1: select range_variable

Claire O'Donnell:112

Sven Mortensen:113

Cesar Garcia:114

studentQuery2: select range_erty

O'Donnell

Mortensen

Garcia

studentQuery3: select range_od

,206-555-0298

,206-555-1130

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

*/

如上一个示例中的 studentQuery8 所示,您有时可能希望所返回序列中的元素仅包含源元素的属性子集。通过使返回的序列尽可能地小一些,可以降低内存需求,并提高查询的执行速度。通过在 select 子句中创建一个匿名类型,并且借助于对象初始值设定项用源元素中的适当属性对该匿名类型进行初始化,可以达到此目的。