11.7.1 需要使用点标记的操作

最明显的必须使用点标记的情形是调用 Reverse 、 ToDictionary 这类没有相应的查询表达
式语法的方法。然而即使查询表达式支持你要使用的查询操作符,也很有可能无法使用你想使用
的特定重载。
例如, Enumerable.Where 包含一个重载,将父序列的索引作为另一个参数传入委托。因此,
要从序列中排除其他项可以这样:

             List<string> list = new List<string>() { "", "", "" };

             var a = list.Where((item, index) => index %  == ).ToList();

             var b = list.Select((Item, Index) => new { Item, Index }).OrderBy(x => x.Item);

11.7.2 使用点标记可能会更简单的查询表达式

         static void Main(string[] args)
{
List<Person> people = new List<Person>();
var a = from person in people
where person.Age >=
select person;
var b = people.Where(e => e.Age >= ).Select(e => e.Name).ToList(); Console.ReadKey();
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

11.7.3 选择查询表达式

         static void Main(string[] args)
{
var a = from defect in SampleData.AllDefects
join subscription in SampleData.AllSubscriptions
on defect.Project equals subscription.Project
select new { defect.Summary, subscription.EmailAddress }; var a2 = SampleData.AllDefects.Join(SampleData.AllSubscriptions,
defect => defect.Project,
subscription => subscription.Project,
(defect, subscription) =>
new { defect.Summary, subscription.EmailAddress });
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();
}
}

11.7 【Linq】在查询表达式和点标记之间作出选择的更多相关文章

  1. Linq之查询表达式语法详解

    1.闲言碎语 由于项目的需要接触到Linq,刚开始有些不适应,好多概念都很模糊.不过经过一段时间的摸索,慢慢地对Linq有了一个更加深入的了解.在此记录一下备忘.      2.查询表达式语法 执行L ...

  2. 2.1 LINQ的查询表达式

    在进行LINQ查询的编写之前,首先需要了解查询表达式.查询表达式是LINQ查询的基础,也是最常用的编写LINQ查询的方法. 查询表达式由查询关键字和对应的操作数组成的表达式整体.其中,查询关键字是常用 ...

  3. linq 在查询表达式中处理异常

    在查询表达式的上下文中可以调用任何方法. 但是,我们建议避免在查询表达式中调用任何会产生副作用(如修改数据源内容或引发异常)的方法. 此示例演示在查询表达式中调用方法时如何避免引发异常,而不违反有关异 ...

  4. linq 在查询表达式中处理 null 值

    此示例显示如何在源集合中处理可能的 null 值. IEnumerable<T> 等对象集合可包含值为 null 的元素. 如果源集合为 null 或包含值为 null 的元素,并且查询不 ...

  5. 二、LINQ之查询表达式基础

    1.查询是什么? 查询是一组指令,描述要从给定数据源(或源)检索的数据以及返回的数据应具有的形状和组织.查询表达式和它所产生的结果不同.

  6. 查询表达式和LINQ to Objects

    查询表达式实际上是由编译器“预处理”为“普通”的C#代码,接着以完全普通的方式进行编译.这种巧妙的发式将查询集合到了语言中,而无须把语义改得乱七八糟 LINQ的介绍 LINQ中的基础概念 降低两种数据 ...

  7. C#复习笔记(4)--C#3:革新写代码的方式(查询表达式和LINQ to object(下))

    查询表达式和LINQ to object(下) 接下来我们要研究的大部分都会涉及到透明标识符 let子句和透明标识符 let子句不过是引入了一个新的范围变量.他的值是基于其他范围变量的.let 标识符 ...

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

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

  9. LINQ(数据查询)

    如果只有空的构造函数,想要对字段进行初始化,可以直接在构造函数后面加上{属性名=值,属性名=值};属性与属性之间用,分割开 //查询所有武学级别大于8的武林高手 //var res = new Lis ...

随机推荐

  1. Linux: 统计代码行数和SVN修改行数的命令

    Mac下同样有效. 1. 统计目录下所有js文件的代码行数. find . -name '*.js' | xargs wc -l 2. 统计SVN的修改行数. svn diff -rBeginRev: ...

  2. Windows移动开发(五)——初始XAML

    关于详细的基本功就先说这么多.后面遇到再补充说明,前面说的都是一些代码和原理方面的东西.接下来说的会有界面和代码结合,会有成就感,由于能真正的做出东西来了. Windows移动开发包含Windows ...

  3. Android---keycode值以及相应名称

    KEYCODE列表 电话键 键名 描写叙述 键值   KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOME 按键Home 3 KEYCODE_ME ...

  4. linux网络測试命令

    ping  192.168.1.103  -c  3 ping我的IP3次 port探測 telnet  192.168.1.103  80 路由跟踪 traceroute  www.imooc.co ...

  5. vbs socket

    http://www.bathome.net/thread-423-1-1.html http://files.cnblogs.com/files/developer-ios/mswinsck.ocx ...

  6. 2本Hadoop技术内幕电子书百度网盘下载:深入理解MapReduce架构设计与实现原理、深入解析Hadoop Common和HDFS架构设计与实现原理

    这是我收集的两本关于Hadoop的书,高清PDF版,在此和大家分享: 1.<Hadoop技术内幕:深入理解MapReduce架构设计与实现原理>董西成 著  机械工业出版社2013年5月出 ...

  7. c# 删除程序占用的文件,强力删除文件,彻底删除文件,解除文件占用

    c# 删除程序占用的文件.清理删除文件.彻底删除文件,解除文件占用 文件打开时,以共享读写模式打开 FileStream inputStream = new FileStream(name, File ...

  8. IOS7中动态计算UILable的高度

    .h文件 #import <UIKit/UIKit.h> @interface UILabel (ContentSize) - (CGSize)contentSize; @end .m文件 ...

  9. Struts 配置文件

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  10. 【BZOJ1306】match循环赛

    预先警告:我的做法代码量比较大 看完题目后看到数据n<=8, 不难想到这题可以写深搜来做 分析 比如说以数据: 3 3 3 3 为例子, 进行了三场比赛:AB AC BC: 我们只要搜索每场比赛 ...