首先添加数据集合

 [Serializable]
public class Racer : IComparable<Racer>, IFormattable
{
public Racer()
{ }
public Racer(string firstName, string lastName, string country, int starts, int wins)
: this(firstName, lastName, country, starts, wins, null, null)
{
}
public Racer(string firstName, string lastName, string country, int starts, int wins, IEnumerable<int> years, IEnumerable<string> cars)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Country = country;
this.Starts = starts;
this.Wins = wins;
this.Years = new List<int>(years);
this.Cars = new List<string>(cars);
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public int Wins { get; set; }
public int Starts { get; set; }
public IEnumerable<string> Cars { get; private set; }
public IEnumerable<int> Years { get; private set; } public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
} public int CompareTo(Racer other)
{
if (other == null) return -;
return string.Compare(this.LastName, other.LastName);
} public string ToString(string format)
{
return ToString(format, null);
} public string ToString(string format,
IFormatProvider formatProvider)
{
switch (format)
{
case null:
case "N":
return ToString();
case "F":
return FirstName;
case "L":
return LastName;
case "C":
return Country;
case "S":
return Starts.ToString();
case "W":
return Wins.ToString();
case "A":
return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}",
FirstName, LastName, Country, Starts, Wins);
default:
throw new FormatException(String.Format("Format {0} not supported", format));
}
}
}
  private static List<Racer> racers;

     public static IList<Racer> GetChampions()
{
if (racers == null)
{
racers = new List<Racer>();
racers.Add(new Racer("Nino", "Farina", "Italy", , , new int[] { }, new string[] { "Alfa Romeo" }));
racers.Add(new Racer("Alberto", "Ascari", "Italy", , , new int[] { , }, new string[] { "Ferrari" }));
racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", , , new int[] { , , , , }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }));
racers.Add(new Racer("Mike", "Hawthorn", "UK", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Phil", "Hill", "USA", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("John", "Surtees", "UK", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Jim", "Clark", "UK", , , new int[] { , }, new string[] { "Lotus" }));
racers.Add(new Racer("Jack", "Brabham", "Australia", , , new int[] { , , }, new string[] { "Cooper", "Brabham" }));
racers.Add(new Racer("Denny", "Hulme", "New Zealand", , , new int[] { }, new string[] { "Brabham" }));
racers.Add(new Racer("Graham", "Hill", "UK", , , new int[] { , }, new string[] { "BRM", "Lotus" }));
racers.Add(new Racer("Jochen", "Rindt", "Austria", , , new int[] { }, new string[] { "Lotus" }));
racers.Add(new Racer("Jackie", "Stewart", "UK", , , new int[] { , , }, new string[] { "Matra", "Tyrrell" }));
racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", , , new int[] { , }, new string[] { "Lotus", "McLaren" }));
racers.Add(new Racer("James", "Hunt", "UK", , , new int[] { }, new string[] { "McLaren" }));
racers.Add(new Racer("Mario", "Andretti", "USA", , , new int[] { }, new string[] { "Lotus" }));
racers.Add(new Racer("Jody", "Scheckter", "South Africa", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Alan", "Jones", "Australia", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Keke", "Rosberg", "Finland", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Niki", "Lauda", "Austria", , , new int[] { , , }, new string[] { "Ferrari", "McLaren" }));
racers.Add(new Racer("Nelson", "Piquet", "Brazil", , , new int[] { , , }, new string[] { "Brabham", "Williams" }));
racers.Add(new Racer("Ayrton", "Senna", "Brazil", , , new int[] { , , }, new string[] { "McLaren" }));
racers.Add(new Racer("Nigel", "Mansell", "UK", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Alain", "Prost", "France", , , new int[] { , , , }, new string[] { "McLaren", "Williams" }));
racers.Add(new Racer("Damon", "Hill", "UK", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Jacques", "Villeneuve", "Canada", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Mika", "Hakkinen", "Finland", , , new int[] { , }, new string[] { "McLaren" }));
racers.Add(new Racer("Michael", "Schumacher", "Germany", , , new int[] { , , , , , , }, new string[] { "Benetton", "Ferrari" }));
racers.Add(new Racer("Fernando", "Alonso", "Spain", , , new int[] { , }, new string[] { "Renault" }));
racers.Add(new Racer("Kimi", "Räikkönen", "Finland", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Lewis", "Hamilton", "UK", , , new int[] { }, new string[] { "McLaren" }));
racers.Add(new Racer("Jenson", "Button", "UK", , , new int[] { }, new string[] { "Brawn GP" }));
racers.Add(new Racer("Sebastian", "Vettel", "Germany", , , new int[] { , }, new string[] { "Red Bull Racing" }));
} return racers;
}

1、where子句

 var racers = from r in Formula1.GetChampions()
where r.Wins > && (r.Country == "Brazil" || r.Country == "Austria")
select r;
var item = Formula1.GetChampions().Where(p => p.Wins > && (p.Country == "Brazil" || p.Country == "Austria")).Select(r => r);

2、根据索引筛选

     var racers = Formula1.GetChampions().
Where((r, index) => r.LastName.StartsWith("A") && index % != );

3、类型筛选

  object[] data = { "one", , , "four", "five",  };
var query = data.OfType<string>();

4、复合的from子句

          var ferrariDrivers = from r in Formula1.GetChampions()
from c in r.Cars
where c == "Ferrari"
orderby r.LastName
select r.FirstName + " " + r.LastName;
var item = Formula1.GetChampions().SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c }).
Where(r => r.Car == "Ferrari").OrderBy(r => r.Racer.LastName).Select(r => r.Racer.FirstName + "" + r.Racer.LastName);

5、排序

 var racers = from r in Formula1.GetChampions()
where r.Country == "Brazil"
orderby r.Wins descending
select r;
var racers2= from r in Formula1.GetChampions()
orderby r.Country,r.LastName,r.FirstName
select r;
var racers3 = Formula1.GetChampions().OrderBy(r => r.Country).ThenBy(r => r.LastName).ThenBy(r => r.FirstName);

6、分组

 var countries = from r in Formula1.GetChampions()
group r by r.Country into g
orderby g.Count() descending, g.Key
where g.Count() >=
select new { Country = g.Key, Count = g.Count() }; var item = Formula1.GetChampions().GroupBy(r => r.Country).OrderByDescending(p => p.Count()).
ThenBy(p => p.Key).Where(p => p.Count() >= ).Select(p => new { Country = p.Key, Count = p.Count() });

7、内链接

   var racers = from r in Formula1.GetChampions()
from y in r.Years
select new
{
Year = y,
Name = r.FirstName + " " + r.LastName
}; var teams = from t in Formula1.GetContructorChampions()
from y in t.Years
select new
{
Year = y,
Name = t.Name
};

8、左连接

  var racersAndTeams =
(from r in racers
join t in teams on r.Year equals t.Year into rt
from t in rt.DefaultIfEmpty()
orderby r.Year
select new
{
Year = r.Year,
Champion = r.Name,
Constructor = t == null ? "no constructor championship" : t.Name
}).Take();

9、zip 合并

  var racerNames = from r in Formula1.GetChampions()
where r.Country == "Italy"
orderby r.Wins descending
select new
{
Name = r.FirstName + " " + r.LastName
}; var racerNamesAndStarts = from r in Formula1.GetChampions()
where r.Country == "Italy"
orderby r.Wins descending
select new
{
LastName = r.LastName,
Starts = r.Starts
}; var racers = racerNames.Zip(racerNamesAndStarts, (first, second) => first.Name + ", starts: " + second.Starts);

10、聚合函数

  var query = from r in Formula1.GetChampions()
let numberYears = r.Years.Count() where numberYears >=
orderby numberYears descending, r.LastName
select new
{
Name = r.FirstName + " " + r.LastName,
TimesChampion = numberYears
}; var countries = (from c in
from r in Formula1.GetChampions()
group r by r.Country into c
select new
{
Country = c.Key,
Wins = (from r1 in c
select r1.Wins).Sum()
}
orderby c.Wins descending, c.Country
select c).Take();

C# LINQ标准查询操作符的更多相关文章

  1. Linq 标准查询操作符三

    本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...

  2. LINQ 标准查询操作符

    本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...

  3. LINQ标准查询操作符详解(转)

     一. 关于LINQ       LINQ 英文全称是“Language-Integrated Query”,中文为“语言集成查询”,它是微软首席架构师.Delphi 之父和C# 之父——Anders ...

  4. LINQ标准查询操作符(三)——Aggregate、Average、Distinct、Except、Intersect、Union、Empty、DefaultIfEmpty、Range、Repeat

    七.聚合操作符 聚合函数将在序列上执行特定的计算,并返回单个值,如计算给定序列平均值.最大值等.共有7种LINQ聚合查询操作符:Aggregate.Average.Count.LongCount.Ma ...

  5. 【LINQ标准查询操作符总结】之聚合操符

    C#  中的LINQ 提供了两种操作方式,查询表达式和查询操作符,所有的查询表达式都有对应的查操作符类替代,查询表达式有点“类” SQL,在代码中写SQL,总觉得不够“优雅”,使用查询操作符就显得“优 ...

  6. LINQ标准查询操作符(四) —AsEnumerable,Cast,OfType,ToArray,ToDictionary,ToList,ToLookup,First,Last,ElementAt

    十.转换操作符 转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以“As”开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合.名称以“To”开头的方法可枚举(即时加载)源集合并 ...

  7. LINQ标准查询操作符(五)

    十二.相等操作符 如果两个序列的对应元素相等且这两个序列具有相同数量的元素,则视这两个序列相等. SequenceEqual方法通过并行地枚举两个数据源并比较相应元素来判断两个序列是否相等.如果两个序 ...

  8. LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、

    四.联接操作符 联接是指将一个数据源对象与另一个数据源对象进行关联或者联合的操作.这两个数据源对象通过一个共同的值或者属性进行关联. LINQ有两个联接操作符:Join和GroupJoin. 1. J ...

  9. LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse

    一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: //查询语法 var query = fro ...

  10. Linq标准查询操作符

     Linq的出现让代码简洁了不少.之前在项目中基本都在使用它,但是没有完整的整理过,今天借这个周末,将其进行整理,方便后期对其的使用.Linq的操作可以分为聚合,连接,转换,元素操作符,相等操作,生成 ...

随机推荐

  1. Alt+~,Alt+Tab

    Alt+~可以很方便的切换同一个软件中的内容. Alt+Tab可以很方便的切换不同软件之间的内容. 熟练使用,很方便.

  2. Linux 时间以及时间间隔的简单处理.

    最近想知道自己的一个部署脚本的耗时, 中午时间看了一下最简单的Linux 时间函数的处理 我这里的处理非常简单, 仅仅是够用而已. 处理过程. 1. 获取当前时间: time1=`date` 或者是 ...

  3. Spring boot+Websocket实例1

    简单的demo https://github.com/callicoder/spring-boot-websocket-chat-demo

  4. 图像人脸检测+人眼检测 (opencv + c++)

    摘要:实现图像中人脸检测,和人眼定位.输出检测标记图像和定位坐标. 工具:vs2015 opencv3  C++ 资源:haarcascade_frontalface_alt2.xml;haarcas ...

  5. WUSTOJ 1239: n皇后问题(Java)

    题目链接:

  6. STM32的I2C特性及架构

    软件模拟协议:使用CPU直接控制通讯引脚(GPIO)的电平,产生出符合通讯协议标准的逻辑. 硬件实现协议:由STM32的I2C片上外设专门负责实现I2C通讯协议,只要配置好该外设,它就会自动根据协议要 ...

  7. Python开发【第七章】:异常处理

    一.异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! #异常处理 list = [&qu ...

  8. python 切换虚拟环境

    每次电脑重启后,都要切入虚拟环境,命令总是忘记.如果使用IDE,可以指定interpeter方便的切换. 首先conda info --env  查看当前有几个环境 激活/禁用环境 source ac ...

  9. C# Winform 设置窗口打开的特效

    https://www.cnblogs.com/mq0036/p/6421946.html using System.Runtime.InteropServices; public class Win ...

  10. VS App_Code文件夹下的类文件不能直接被调用的解决方法

    如下图所示,新建的类不能直接使用,会显示报错,检查命名空间什么的,未果 通过百度搜索,发现这么一篇文章:https://blog.csdn.net/younghaiqing/article/detai ...