您的位置:首页 > 其它

SelectMany等LINQ运算符的使用

2014-12-05 13:29 232 查看
public class Racer : IComparable<Racer>, IFormattable
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Wins { get; set; }
public string Country { get; set; }
public int Starts { get; set; }
public IEnumerable<string> Cars { get; set; }
public IEnumerable<int> Years { get; set; }

public Racer(string firstName,string lastName,string country,int wins,int starts,IEnumerable<int> years,IEnumerable<string> cars)
{
FirstName = firstName;
LastName = lastName;
Wins = wins;
Country = country;
Starts = starts;
Cars = new List<string>(cars);
Years = new List<int>(years);
}

public Racer(string firstName, string lastName, int wins, string country, int starts)
: this(firstName, lastName,country, wins, starts,null, null)
{

}

public override string ToString()
{
return string.Format("{0}_{1}", FirstName, LastName);
}

public int CompareTo(Racer other)
{
if (other == null) return -1;
int result = string.Compare(FirstName, other.FirstName);
if (result == 0)
result = string.Compare(LastName, other.LastName);
return result;
}

public string ToString(string format, IFormatProvider formatProvider)
{
format = format ?? "N";
switch(format)
{
case "N":
return ToString();
case "C":
return string.Format("{0} Country:{1}", ToString(), Country);
case "S":
return string.Format("{0} Starts:{1}", ToString(), Starts);
case "W":
return string.Format("{0} Wins:{1}", ToString(), Wins);
case "Y":
var result=ToString();
foreach(var item in Years)
{
result += item;
}
return result;
default:
throw new FormatException(string.Format("Format {0} not supported", format));
}
}
}


public class Team
{
public string Name { get; private set; }
public IEnumerable<int> Years { get; private set; }

public Team(string name,params int[] years)
{
Name = name;
Years = new List<int>(years);
}
}


public static class Formula1
{
private static List<Racer> racers;
private static List<Team> teams;

public static List<Racer> Racers
{
get { return racers ?? (racers = new List<Racer>(40) { new Racer("Nino", "Farina", "Italy", 33, 5, new[] { 1950 }, new[] { "Alfa Romeo" }), new Racer("Alberto", "Ascari", "Italy", 30, 10, new[] { 1952, 1953 }, new[] { "Ferrari" }), new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new[] { 1951, 1954, 1955, 1956, 1957 }, new[] { "Alfa Romeo", "Maserati" }), new Racer("Mike", "Hawthorn", "UK", 45, 3, new[] { 1958 }, new[] { "Ferrari" }) }); }
}

public static List<Team> Teams
{
get
{
return teams ?? (teams = new List<Team>{new Team("Vanwall",1958),
new Team("Cooper", 1959, 1960),
new Team("Ferrari", 1961, 1964, 1975, 1976, 1977, 1979, 1982,1983, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008),
new Team("BRM", 1962),
new Team("Lotus", 1963, 1965, 1968, 1970, 1972, 1973, 1978),
new Team("Brabham", 1966, 1967),
new Team("Matra", 1969),
new Team("Tyrrell", 1971)});
}
}
}


static void Main(string[] args)
{
var result = from r in Formula1.Racers
from c in r.Cars
where c.Equals("Ferrari")
orderby r.FirstName
select r.FirstName + " " + r.LastName;

foreach(var item in result)
{
Console.WriteLine(item);
}

var result2 = Formula1.Racers.SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c })
.Where(r => r.Car.Equals("Ferrari")).OrderBy(r => r.Racer.FirstName).Select(r => r.Racer.FirstName + " " + r.Racer.LastName);

Console.ReadKey();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: