11.2.4 Cast 、 OfType 和显式类型的范围变量
这两个操作符很相似:都可以处理任意非类型化的序列(它们是非泛型 IEnumerable 类的扩展方法),并返回强类型的序列。
Cast 通过把每个元素都转换为目标类型(遇到不是正确类型的任何元素的时候,就会出错)来处理,而 OfType 首先进行一个测试,以跳过任何具有错误类型的元素。
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList() { "First", "Second", "Third" };
IEnumerable<string> strings = list.Cast<string>();
foreach (var item in strings)
{
Console.WriteLine(item);
} list = new ArrayList() { , "Second", };
IEnumerable<int> ints = list.OfType<int>();
foreach (var item in ints)
{
Console.WriteLine(item);
} Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList() { "First", "Second", "Third" };
var strings = from string m in list
select m.Substring(, );
foreach (var item in strings)
{
Console.WriteLine(item);
} list = new ArrayList() { "First", "Second", "Third" };
foreach (var item in list.Cast<string>().Select(a => a.Substring(, )))
{
Console.WriteLine(item);
} Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
User tim = SampleData.Users.TesterTim; var query = from m in SampleData.AllDefects
where m.Status != Status.Closed
where m.AssignedTo == tim
select m.Summary; foreach (var item in query)
{
Console.WriteLine(item);
} Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
User tim = SampleData.Users.TesterTim; var query = from m in SampleData.AllDefects
where m.Status != Status.Closed
where m.AssignedTo == tim
orderby m.Severity descending, m.LastModified
select m; foreach (var item in query)
{
Console.WriteLine("{0} : {1} ({2:d})", item.Severity, item.Summary, item.LastModified);
} Console.ReadKey();
}
}
11.4 let 子句和透明标识符
class Program
{
static void Main(string[] args)
{
var query = from user in SampleData.AllUsers
let Length = user.Name.Length
orderby Length
select new { Name = user.Name, Length = Length }; var query2 = SampleData.AllUsers
.Select(user => new { user, Length = user.Name.Length })
.OrderBy(z => z.Length)
.Select(z => new { Name = z.user.Name, Length = z.Length }); foreach (var item in query)
{
Console.WriteLine("{0} {1}", item.Name, item.Length);
}
Console.ReadKey();
}
}
SampleData代码,如下
public class SampleData
{
static List<Defect> defects;
static List<User> users;
static List<Project> projects;
static List<NotificationSubscription> subscriptions; public static readonly DateTime Start = May();
public static readonly DateTime End = May(); public static IEnumerable<Defect> AllDefects
{
get { return defects; }
} public static IEnumerable<User> AllUsers
{
get { return users; }
} public static IEnumerable<Project> AllProjects
{
get { return projects; }
} public static IEnumerable<NotificationSubscription> AllSubscriptions
{
get { return subscriptions; }
} public static class Projects
{
public static readonly Project SkeetyMediaPlayer = new Project { Name = "Skeety Media Player" };
public static readonly Project SkeetyTalk = new Project { Name = "Skeety Talk" };
public static readonly Project SkeetyOffice = new Project { Name = "Skeety Office" };
} public static class Users
{
public static readonly User TesterTim = new User("Tim Trotter", UserType.Tester);
public static readonly User TesterTara = new User("Tara Tutu", UserType.Tester);
public static readonly User DeveloperDeborah = new User("Deborah Denton", UserType.Developer);
public static readonly User DeveloperDarren = new User("Darren Dahlia", UserType.Developer);
public static readonly User ManagerMary = new User("Mary Malcop", UserType.Manager);
public static readonly User CustomerColin = new User("Colin Carton", UserType.Customer);
} static SampleData()
{
projects = new List<Project>
{
Projects.SkeetyMediaPlayer,
Projects.SkeetyTalk,
Projects.SkeetyOffice
}; users = new List<User>
{
Users.TesterTim,
Users.TesterTara,
Users.DeveloperDeborah,
Users.DeveloperDarren,
Users.ManagerMary,
Users.CustomerColin
}; subscriptions = new List<NotificationSubscription>
{
new NotificationSubscription { Project=Projects.SkeetyMediaPlayer, EmailAddress="media-bugs@skeetysoft.com" },
new NotificationSubscription { Project=Projects.SkeetyTalk, EmailAddress="talk-bugs@skeetysoft.com" },
new NotificationSubscription { Project=Projects.SkeetyOffice, EmailAddress="office-bugs@skeetysoft.com" },
new NotificationSubscription { Project=Projects.SkeetyMediaPlayer, EmailAddress="theboss@skeetysoft.com"}
}; defects = new List<Defect>
{
new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "MP3 files crash system",
Severity = Severity.Showstopper,
AssignedTo = Users.DeveloperDarren,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.DeveloperDeborah,
Summary = "Text is too big",
Severity = Severity.Trivial,
AssignedTo = null,
Status = Status.Closed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "Sky is wrong shade of blue",
Severity = Severity.Minor,
AssignedTo = Users.TesterTara,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.DeveloperDarren,
Summary = "Can't play files more than 200 bytes long",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDarren,
Status = Status.Reopened,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Installation is slow",
Severity = Severity.Trivial,
AssignedTo = Users.TesterTim,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "DivX is choppy on Pentium 100",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDarren,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDeborah,
Summary = "Client acts as virus",
Severity = Severity.Showstopper,
AssignedTo = null,
Status = Status.Closed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.DeveloperDarren,
Summary = "Subtitles only work in Welsh",
Severity = Severity.Major,
AssignedTo = Users.TesterTim,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "Voice recognition is confused by background noise",
Severity = Severity.Minor,
AssignedTo = null,
Status = Status.Closed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "User interface should be more caramelly",
Severity = Severity.Trivial,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "Burning a CD makes the printer catch fire",
Severity = Severity.Showstopper,
AssignedTo = null,
Status = Status.Closed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.TesterTara,
Summary = "Peer to peer pairing passes parameters poorly",
Severity = Severity.Minor,
AssignedTo = Users.DeveloperDarren,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDarren,
Summary = "Delay when sending message",
Severity = Severity.Minor,
AssignedTo = Users.TesterTara,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "Volume control needs to go to 11",
Severity = Severity.Minor,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "Splash screen fades too quickly",
Severity = Severity.Minor,
AssignedTo = Users.TesterTara,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDeborah,
Summary = "Text box doesn't keep up with fast typing",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDarren,
Summary = "Password displayed in plain text",
Severity = Severity.Showstopper,
AssignedTo = null,
Status = Status.Closed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Play button points the wrong way",
Severity = Severity.Major,
AssignedTo = Users.TesterTim,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "Wizard needed for CD burning",
Severity = Severity.Minor,
AssignedTo = Users.CustomerColin,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "Subtitles don't display during fast forward",
Severity = Severity.Trivial,
AssignedTo = Users.DeveloperDarren,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.DeveloperDarren,
Summary = "Memory leak when watching Memento",
Severity = Severity.Trivial,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDeborah,
Summary = "Profile screen shows login count of -1",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Server crashes under heavy load (3 users)",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTara,
Summary = "Unable to connect to any media server",
Severity = Severity.Showstopper,
AssignedTo = Users.DeveloperDarren,
Status = Status.Reopened,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.DeveloperDeborah,
Summary = "UI turns black and white when playing old films",
Severity = Severity.Minor,
AssignedTo = Users.TesterTara,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "Password reset changes passwords for all users",
Severity = Severity.Showstopper,
AssignedTo = null,
Status = Status.Closed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Modern music sounds rubbish",
Severity = Severity.Trivial,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Webcam makes me look bald",
Severity = Severity.Showstopper,
AssignedTo = Users.TesterTim,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "Sound is distorted when speakers are underwater",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDarren,
Summary = "Japanese characters don't display properly",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTara,
Summary = "Video takes 100% of CPU",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "DVD Easter eggs unavailable",
Severity = Severity.Trivial,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "Transparency is high for menus to be readable",
Severity = Severity.Minor,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "About box is missing version number",
Severity = Severity.Minor,
AssignedTo = Users.CustomerColin,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Logs record confidential conversations",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDarren,
Status = Status.Reopened,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.DeveloperDeborah,
Summary = "Profanity filter is too aggressive",
Severity = Severity.Minor,
AssignedTo = Users.TesterTara,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTara,
Summary = "Full screen mode fails on dual monitors",
Severity = Severity.Minor,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.CustomerColin,
Summary = "Visualization hypnotises pets",
Severity = Severity.Minor,
AssignedTo = Users.DeveloperDeborah,
Status = Status.Accepted,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyTalk,
Created = May(),
CreatedBy = Users.ManagerMary,
Summary = "Resizing while typing loses input",
Severity = Severity.Trivial,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTim,
Summary = "Network is saturated when playing WAV file",
Severity = Severity.Minor,
AssignedTo = Users.TesterTim,
Status = Status.Fixed,
LastModified = May()
}, new Defect
{
Project = Projects.SkeetyMediaPlayer,
Created = May(),
CreatedBy = Users.TesterTara,
Summary = "Media library tells user to keep the noise down",
Severity = Severity.Major,
AssignedTo = Users.DeveloperDarren,
Status = Status.Created,
LastModified = May()
}
};
} public static DateTime May(int day)
{
return new DateTime(, , day);
} }
public enum UserType : byte
{
Customer,
Developer,
Tester,
Manager,
}
public class Defect
{
public Project Project { get; set; }
/// <summary>
/// Which user is this defect currently assigned to? Should not be null until the status is Closed.
/// </summary>
public User AssignedTo { get; set; }
public string Summary { get; set; }
public Severity Severity { get; set; }
public Status Status { get; set; }
public DateTime Created { get; set; }
public DateTime LastModified { get; set; }
public User CreatedBy { get; set; }
public int ID { get; private set; } public Defect()
{
ID = StaticCounter.Next();
} public override string ToString()
{
return string.Format("{0,2}: {1}\r\n ({2:d}-{3:d}, {4}/{5}, {6} -> {7})",
ID, Summary, Created, LastModified, Severity, Status, CreatedBy.Name,
AssignedTo == null ? "n/a" : AssignedTo.Name);
}
}
public class NotificationSubscription
{
/// <summary>
/// Project for which this subscriber is notified
/// </summary>
public Project Project { get; set; } /// <summary>
/// The address to send the notification to
/// </summary>
public string EmailAddress { get; set; }
}
public class Project
{
public string Name { get; set; } public override string ToString()
{
return string.Format("Project: {0}", Name);
}
}
public enum Severity : byte
{
Trivial,
Minor,
Major,
Showstopper,
}
public static class StaticCounter
{
static int next = ;
public static int Next()
{
return next++;
}
}
public enum Status : byte
{
/// <summary>
/// Defect has been opened, but not verified as reproducible or an issue.
/// </summary>
Created,
/// <summary>
/// Defect has been verified as an issue requiring work.
/// </summary>
Accepted,
/// <summary>
/// Defect has been fixed in code, but not verified other than through developer testing.
/// </summary>
Fixed,
/// <summary>
/// Defect was fixed, but has now been reopened due to failing verification.
/// </summary>
Reopened,
/// <summary>
/// Defect has been fixed and tested; the fix is satisfactory.
/// </summary>
Closed,
}
public class User
{
public string Name { get; set; }
public UserType UserType { get; set; } public User(string name, UserType userType)
{
Name = name;
UserType = userType;
} public override string ToString()
{
return string.Format("User: {0} ({1})", Name, UserType);
}
}
static class Extensions
{
public static Dummy<T> Where<T>(this Dummy<T> dummy,
Func<T, bool> predicate)
{
Console.WriteLine("Where called");
return dummy;
}
}
class Dummy<T>
{
public Dummy<U> Select<U>(Func<T, U> selector)
{
Console.WriteLine("Select called");
return new Dummy<U>();
}
}
public class DateTimeRange : IEnumerable<DateTime>
{
private readonly DateTime start;
private readonly DateTime end; public DateTimeRange(DateTime start, DateTime end)
{
this.start = start;
this.end = end;
} public IEnumerator<DateTime> GetEnumerator()
{
for (DateTime current = start; current <= end; current = current.AddDays())
{
yield return current;
}
} IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}