第一次学着用Linq的盆友们,可以看看哈。。。。

代码 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->class SelectSample1
{
static void Main()
{
//Create the data source
List<int> Scores = new List<int>() { , , , }; // Create the query.
IEnumerable<int> queryHighScores =
from score in Scores
where score >
select score; // Execute the query.
foreach (int i in queryHighScores)
{
Console.Write(i + " ");
}
}
}

2、集合序列化:

        private XElement SerializeDesignerItems(IEnumerable<DesignerItem> designerItems)
{
XElement serializedItems = new XElement("DesignerItems",
from item in designerItems
let contentXaml = XamlWriter.Save(((DesignerItem)item).Content)
select new XElement("DesignerItem",
new XElement("Left", Canvas.GetLeft(item)),
new XElement("Top", Canvas.GetTop(item)),
new XElement("Width", item.Width),
new XElement("Height", item.Height),
new XElement("ID", item.ID),
new XElement("zIndex", Canvas.GetZIndex(item)),
new XElement("IsGroup", item.IsGroup),
new XElement("ParentID", item.ParentID),
new XElement("Content", contentXaml)
)
); return serializedItems;
}

下面的示例演示了 select 子句可能采用的所有不同形式。在每个查询中,请注意 select 子句和查询变量(studentQuery1、studentQuery2 等)的类型之间的关系。

    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=, Scores= new List<int>() {, , , }},
new Student {First="Claire", Last="O'Donnell", ID=, Scores= new List<int>() {, , , }},
new Student {First="Sven", Last="Mortensen", ID=, Scores= new List<int>() {, , , }},
new Student {First="Cesar", Last="Garcia", ID=, Scores= new List<int>() {, , , }},
}; // Separate data source for contact info.
List<ContactInfo> contactList = new List<ContactInfo>()
{
new ContactInfo {ID=, Email="SvetlanO@Contoso.com", Phone="206-555-0108"},
new ContactInfo {ID=, Email="ClaireO@Contoso.com", Phone="206-555-0298"},
new ContactInfo {ID=, Email="SvenMort@Contoso.com", Phone="206-555-1130"},
new ContactInfo {ID=, 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 >
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 >
select student.Last; Console.WriteLine("\r\n 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 >
select student.GetContactInfo(app, student.ID); Console.WriteLine("\r\n 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 >
select student.Scores[]; Console.WriteLine("\r\n 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 >
select student.Scores[] * 1.1; Console.WriteLine("\r\n 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 >
select student.Scores.Average(); Console.WriteLine("\r\n 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 >
select new { student.First, student.Last }; Console.WriteLine("\r\n 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 >
select new ScoreInfo
{
Average = student.Scores.Average(),
ID = student.ID
}; Console.WriteLine("\r\n 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() >
join ci in app.contactList on student.ID equals ci.ID
select ci; Console.WriteLine("\r\n 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
*/

最新文章

  1. 夺命雷公狗-----React_native---4---初始化项目
  2. java:关于继承变量的值问题
  3. kmp模板,线性完成pos
  4. qt信号signal和槽slot机制
  5. js 数组中随机出来N组
  6. asp.net Calendar 日历控件用法
  7. php register_shutdown_function
  8. UIImagePickerController从拍照、图库、相册获取图片
  9. php统计文件夹大小
  10. C# dev gridcontrol中添加checkbox复选框
  11. 学习笔记TF026:多层感知机
  12. html5中新增的非主体结构的元素
  13. 深入学习Redis(5):集群
  14. Vue利用canvas实现移动端手写板
  15. lua分割字符串
  16. LeetCode--035--搜索插入位置(java)
  17. ERROR 1215 (HY000): Cannot add foreign key constraint
  18. 【python】Python的安装和配置
  19. Oracle sql 统计
  20. .NET并行计算和并发4-Thread-Relative Static Fields and Data Slots

热门文章

  1. [Javascript] Avoid Accidental Returns of New State by using the void Keyword
  2. Node.js安装,多版本管理以及修改npm下载的镜像源
  3. PKUSC2019 改题记录
  4. 洛谷 P1638 逛画展 题解
  5. 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
  6. CodefChef September Challenge 2019 题解
  7. 数据结构实验之查找二:平衡二叉树 (SDUT 3374)
  8. 《挑战30天C++入门极限》C++运算符重载转换运算符
  9. 模板 - 数学 - 数论 - 扩展Euler定理
  10. tecplot不能导入fluent多面体的解决办法