完整的linQ查询(一看就懂)
输出效果
using System;
using System.Collections.Generic;
using System.Linq;
//namespace LinQtest1
//{
// class Program
// {
// static void Main(string[] args)
// {
// Console.WriteLine("Hello World!");
// }
// }
//}
#region linq查询测试1
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//namespace LinqApplication
//{
//
// //people 人们类 . 这里类似数据库表的,entity框架文件。
// //下面的类中,有3条数据。名字, 姓氏,国家。
// // class People
// // {
// // /// <summary>
// // /// 名字
// // /// </summary>
// // public string FirstName { get; set; } // 人们类中的成员 变量。 firstname 名字 字符串类型。
// // /// <summary>
// // /// 姓氏
// // /// </summary>
// // public string LastName { get; set; }
// // /// <summary>
// // /// 国家
// // /// </summary>
// // public string Country { get; set; }
// // }
// // class Program
// // {
// // // 主函数入口. 可以控制台打印出来结果的代码。
// // static void Main(string[] args)
// // { // 这是new了一个实例对象。 上面人类的实例对象。 people的实例对象代码。
// // List<People> listPeople = new List<People>()
// //{ // 3条同样的数据代码。
// //new People{FirstName="获取Firstname名字",LastName="Ma姓氏获取",Country="China1"},// 数据1
// //new People{FirstName="Alan",LastName="Guo",Country="Japan"},
// //new People{FirstName="chenggong",LastName="Ma",Country="China1"} // 数据2
// //};
// // List<People> result = new List<People>();
// // //foreach 循环代码 在listpeople 中。
// // foreach (People p in listPeople)
// // {
// // // 如果 国家 等于 中国1这个条件,那么后面就打印输出 数据1 和 数据2
// // if (p.Country == "China1")
// // {
// // result.Add(p);
// // }
// // }
// // foreach (People p in result)
// // {
// // Console.WriteLine("Your name is :" + p.FirstName + " " + p.LastName);
// // }
// // Console.ReadKey();
// // }
//
// }
//}
#endregion
#region tolist() 集合程序案例
////3.获取列表
////ToList()
//List<Course> list1 = _context.Courses.Where(q => q.AddTime > DateTime.Now)
// .ToList();
////4.获取部分列表
////Select()
//object objList = _context.Courses.Select(q => new
//{
// q.CourseId,
// q.Title
//}).ToList();
#endregion
#region 完整的linQ查询操作
class LINQQueryExpressions
{
static void Main()
{
// Specify the data source. 1 创建数据源.
int[] scores = new int[] { 97, 92, 81, 60 };// 这是创建了一个整数类型 int 的数据源。
// Define the query expression.2 定义查询表达式。
IEnumerable<int> scoreQuery =
from score in scores
where score > 80
select score;
// Execute the query. 3 执行查询. 在 foreach 语句中执行查询。
foreach (int i in scoreQuery)
{
Console.Write(i + " ");
}
}
}
// Output: 97 92 81
#endregion