{"id":97,"date":"2010-05-20T19:51:51","date_gmt":"2010-05-20T19:51:51","guid":{"rendered":"e2ef524fbf3d9fe611d5a8e90fefdc9c"},"modified":"2010-05-20T19:51:51","modified_gmt":"2010-05-20T19:51:51","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/97","title":{"rendered":"LINQ\u4f53\u9a8c\u7cfb\u5217\u6587\u7ae0"},"content":{"rendered":"<p>\u603b\u4f53\u6765\u8bf4\uff0cVisual Studio 2008\u548c.NET 3.5\u662f\u5efa\u7acb\u5728.NET2.0\u6838\u5fc3\u7684\u57fa\u7840\u4e4b\u4e0a\uff0c.NET2.0\u6838\u5fc3\u672c\u8eab\u5c06\u4e0d\u518d\u53d8\u5316\uff08\u5982\u679c\u4e0d\u4e86\u89e3.NET2.0\u7684\u670b\u53cb\uff0c\u8bf7\u53c2\u770bMSDN\u6216\u8005\u4e00\u4e9b\u7ecf\u5178\u7684\u4e66\u7c4d\uff09\uff0cC# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u5728.NET2.0\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u6539\u8fdb\uff0c\u8fd9\u4e9b\u6539\u8fdb\u7684\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u6211\u4eec\u7f16\u5199\u7a0b\u5e8f\u3002\u5173\u4e8eC# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u5728\u535a\u5ba2\u56ed\u91cc\u7684\u5f88\u591a\u670b\u53cb\u90fd\u4ecb\u7ecd\u4e86\uff0c\u6211\u5728\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u8bb0\u5f55\u81ea\u5df1\u6240\u5b66\u7684\u4e1c\u897f\uff0c\u4e5f\u4e3a\u540e\u9762\u7684LINQ\u6253\u4e0b\u57fa\u7840\u3002<\/p>\n<p>C# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u5c5e\u6027(Auto-Implemented Properties)<\/li>\n<li>\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf(Local Variable Type Inference)<\/li>\n<li>\u533f\u540d\u7c7b\u578b(Anonymous Types)<\/li>\n<li>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668(Object and Collection Initializers)<\/li>\n<li>\u6269\u5c55\u65b9\u6cd5(Extension Methods)<\/li>\n<li>Lambda\u8868\u8fbe\u5f0f\u548cLambda\u8868\u8fbe\u5f0f\u6811 (Lambda Expression and Lambda Expression Trees)<\/li>\n<\/ul>\n<h1>\u81ea\u52a8\u5c5e\u6027(Auto-Implemented Properties)<\/h1>\n<p>\u81ea\u52a8\u5c5e\u6027\u53ef\u4ee5\u907f\u514d\u539f\u6765\u8fd9\u6837\u6211\u4eec\u624b\u5de5\u58f0\u660e\u4e00\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\u4ee5\u53ca\u7f16\u5199get\/set\u903b\u8f91\uff0c\u5728VS2008\u4e2d\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7f16\u5199\u4e00\u4e2a\u7c7b\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5730\u751f\u6210\u79c1\u6709\u53d8\u91cf\u548c\u9ed8\u8ba4\u7684get\/set \u64cd\u4f5c\u3002\u4f60\u4e5f\u53ef\u4ee5\u5206\u522b\u5b9a\u4e49get\u548cset\u7684&ldquo;protected&rdquo;\u7b49\u8bbf\u95ee\u7ea7\u522b\u3002<\/p>\n<p>\u5728.Net2.0\u6846\u67b6\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\u4e00\u4e2aUser\u7c7b\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">public class <\/span>\r\n<span style=\"color: #2b91af\">User\r\n<\/span>\r\n{\r\n    <span style=\"color: blue\">private int <\/span>\r\n_id;\r\n    <span style=\"color: blue\">private string <\/span>\r\n_name;\r\n    <span style=\"color: blue\">private int <\/span>\r\n_age;\r\n    <span style=\"color: blue\">public int <\/span>\r\nId\r\n    {\r\n        <span style=\"color: blue\">get <\/span>\r\n{ <span style=\"color: blue\">return <\/span>\r\n_id; }\r\n        <span style=\"color: blue\">set <\/span>\r\n{ _id = <span style=\"color: blue\">value<\/span>\r\n; }\r\n    }\r\n    <span style=\"color: blue\">public string <\/span>\r\nName\r\n    {\r\n        <span style=\"color: blue\">get <\/span>\r\n{ <span style=\"color: blue\">return <\/span>\r\n_name; }\r\n        <span style=\"color: blue\">set <\/span>\r\n{ _name = <span style=\"color: blue\">value<\/span>\r\n; }\r\n    }\r\n    <span style=\"color: blue\">public int <\/span>\r\nAge\r\n    {\r\n        <span style=\"color: blue\">get <\/span>\r\n{ <span style=\"color: blue\">return <\/span>\r\n_age; }\r\n        <span style=\"color: blue\">set <\/span>\r\n{ _age = <span style=\"color: blue\">value<\/span>\r\n; }\r\n    }\r\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u8fd9\u6837\u7b80\u5316\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">public class <\/span>\r\n<span style=\"color: #2b91af\">User\r\n<\/span>\r\n{\r\n    <span style=\"color: blue\">public int <\/span>\r\nId { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public string <\/span>\r\nName { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public int <\/span>\r\nAge { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n}<\/pre>\n<p>\u50cf\u4e0a\u9762\u8fd9\u6837\u7684\u7a7a\u7684get\/set\u5c5e\u6027\u7684\u8bdd\uff0c\u5b83\u4f1a\u81ea\u52a8\u4e3a\u4f60\u5728\u7c7b\u4e2d\u751f\u6210\u4e00\u4e2a\u79c1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u5b9e\u73b0\u4e00\u4e2a\u516c\u5f00\u7684getter \u548csetter\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528.NET\u5f00\u53d1\u73af\u5883\u6240\u63d0\u4f9b\u7684ildasm.exe\uff08IL\u4ee3\u7801\u53cd\u6c47\u7f16\u5668\uff09\u5de5\u5177\u6765\u5206\u6790\u7a0b\u5e8f\u96c6\u6216\u8005\u6a21\u5757\u7684\u5185\u5bb9\u3002\u6211\u5c31\u4e0d\u8d34\u56fe\u4e86\u3002<\/p>\n<h1>\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf(Local Variable Type Inference)<\/h1>\n<p>C#3.0\u5f15\u8fdb\u4e86var\u8fd9\u4e2a\u65b0\u5173\u952e\u5b57\uff0c\u5728\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u65f6\u53ef\u7528\u4e8e\u66ff\u4ee3\u539f\u5148\u7684\u7c7b\u578b\u540d\uff0c\u5373\u5f53\u4e00\u4e2a\u53d8\u91cf\u58f0\u660e\u6807\u8bc6\u4e3avar\u7c7b\u578b\u5e76\u4e14\u8be5\u8303\u56f4\u57df\u4e2d\u6ca1\u6709var\u540d\u79f0\u7c7b\u578b\u5b58\u5728\uff0c\u90a3\u4e48\u8fd9\u4e2a\u58f0\u660e\u5c31\u79f0\u4e3a\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf\u3002\u5982\u4e0b\uff08\u7b49\u540c\u4e8e\/\/\u540e\u9762\u7684\u663e\u5f0f\u58f0\u660e\uff09\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\ni = 5;<span style=\"color: green\">\/\/int<\/span>\r\n\r\n<span style=\"color: blue\">var <\/span>\r\nj = 23.56;<span style=\"color: green\">\/\/double<\/span>\r\n\r\n<span style=\"color: blue\">var <\/span>\r\nk = <span style=\"color: #a31515\">&quot;C Sharp&quot;<\/span>\r\n;<span style=\"color: green\">\/\/string<\/span>\r\n\r\n<span style=\"color: blue\">var <\/span>\r\nx;<span style=\"color: green\">\/\/\u9519\u8bef<\/span>\r\n\r\n<span style=\"color: blue\">var <\/span>\r\ny = <span style=\"color: blue\">null<\/span>\r\n;<span style=\"color: green\">\/\/\u9519\u8bef<\/span>\r\n\r\n<span style=\"color: blue\">var <\/span>\r\nz = { 1, 2, 3 };<span style=\"color: green\">\/\/\u9519\u8bef<\/span>\r\n<\/pre>\n<p>\u5728\u8c03\u8bd5\u72b6\u6001\u4e0b\uff0c\u7f16\u8bd1\u5668\u89e3\u91ca\u5982\u4e0b<\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ3.gif\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" alt=\"\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf\u8c03\u8bd5\" height=\"237\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ3.gif\" width=\"550\" \/><\/a>\n<\/p>\n<p><em>\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf\u8981\u70b9<\/em>\n<\/p>\n<ol>\n<li>var\u4e3a\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u6839\u636e\u540e\u9762\u7684\u521d\u59cb\u5316\u8bed\u53e5\u81ea\u52a8\u63a8\u65ad\u7c7b\u578b\uff0c\u8fd9\u4e2a\u7c7b\u578b\u4e3a\u5f3a\u7c7b\u578b\u3002<\/li>\n<li>\u521d\u59cb\u5316\u8bed\u53e5\u5fc5\u987b\u4e3a\u8868\u8fbe\u5f0f\uff0c\u4e0d\u53ef\u4ee5\u4e3a\u7a7a\u3002\u4e14\u7f16\u8bd1\u65f6\u53ef\u4ee5\u63a8\u65ad\u7c7b\u578b\u3002\u4e00\u65e6\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u53ea\u53ef\u4ee5\u5b58\u50a8\u8fd9\u79cd\u7c7b\u578b\u3002<\/li>\n<li>var\u58f0\u660e\u7684\u4ec5\u9650\u4e8e\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u53ef\u7528\u4e8e\u5b57\u6bb5\u3002\u4ea6\u53ef\u4ee5\u7528\u4e8efor\uff0cforeach\uff0cusing \u7b49\u8bed\u53e5\u4e2d\u3002<\/li>\n<li>\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u9690\u542b\u7c7b\u578b\u3002<\/li>\n<li>\u521d\u59cb\u5316\u8bed\u53e5\u4e0d\u80fd\u662f\u4e00\u4e2a\u81ea\u8eab\u7684\u5bf9\u8c61\u6216\u8005\u96c6\u5408\u521d\u59cb\u5316\u5668\uff0c\u4f46\u662f\u4ed6\u53ef\u4ee5\u662f\u5305\u542b\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u521d\u59cb\u5316\u5668\u7684\u4e00\u4e2anew\u8868\u8fbe\u5f0f\u3002<\/li>\n<li>\u5982\u679c\u5c40\u90e8\u53d8\u91cf\u58f0\u660e\u5305\u542b\u4e86\u591a\u4e2a\u58f0\u660e\u7b26\uff0c\u5176\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\u3002<\/li>\n<\/ol>\n<h1>\u533f\u540d\u7c7b\u578b(Anonymous Types)<\/h1>\n<p>\u533f\u540d\u7c7b\u578b\u5141\u8bb8\u5b9a\u4e49\u884c\u5185\u7c7b\u578b\uff0c\u65e0\u987b\u663e\u5f0f\u5b9a\u4e49\u7c7b\u578b\u3002\u5e38\u548cvar\u914d\u5408\u4f7f\u7528\u6765\u58f0\u660e\u533f\u540d\u7c7b\u578b\u3002<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\np1 = <span style=\"color: blue\">new <\/span>\r\n{ Id = 1, Name = <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n, Age = 22 };<span style=\"color: green\">\/\/\u5c5e\u6027\u4e5f\u4e0d\u9700\u8981\u7533\u660e<\/span>\r\n\r\n<span style=\"color: blue\">var <\/span>\r\np2 = <span style=\"color: blue\">new <\/span>\r\n{ Id = 2, Name = <span style=\"color: #a31515\">&quot;XieQing&quot;<\/span>\r\n, Age = 25 };\r\np1 = p2;<span style=\"color: green\">\/\/p1,p2\u7ed3\u6784\u76f8\u540c\uff0c\u53ef\u4ee5\u4e92\u76f8\u8d4b\u503c<\/span>\r\n<\/pre>\n<p><img decoding=\"async\" alt=\"\u533f\u540d\u7c7b\u578b\u8c03\u8bd5\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ4.gif\" \/><\/p>\n<p>\u5728\u8fd9\u91cc\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3ap1,p2\u76f8\u5f53\u4e8e\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">public class <\/span>\r\n<span style=\"color: #2b91af\">SomeType<\/span>\r\n\r\n{\r\n    <span style=\"color: blue\">public int <\/span>\r\nId { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public string <\/span>\r\nName { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public int <\/span>\r\nAge { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n}<\/pre>\n<p>\u90a3\u4e48\u6570\u7ec4\u600e\u4e48\u5b9a\u4e49\u5462\uff1f\u4f7f\u7528&quot;new[]&quot;\u5173\u952e\u5b57\u6765\u58f0\u660e\u6570\u7ec4\uff0c\u52a0\u4e0a\u6570\u7ec4\u7684\u521d\u59cb\u503c\u5217\u8868\u3002\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\nintArray = <span style=\"color: blue\">new<\/span>\r\n[] { 2, 3, 5, 6 };\r\n<span style=\"color: blue\">var <\/span>\r\nstrArray = <span style=\"color: blue\">new<\/span>\r\n[] { <span style=\"color: #a31515\">&quot;Hello&quot;<\/span>\r\n, <span style=\"color: #a31515\">&quot;World&quot; <\/span>\r\n};\r\n<span style=\"color: blue\">var <\/span>\r\nanonymousTypeArray = <span style=\"color: blue\">new<\/span>\r\n[] \r\n{ \r\n    <span style=\"color: blue\">new <\/span>\r\n{ Name = <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n, Age = 22 }, \r\n    <span style=\"color: blue\">new <\/span>\r\n{ Name = <span style=\"color: #a31515\">&quot;XieQing&quot;<\/span>\r\n, Age = 25 } \r\n};\r\n<span style=\"color: blue\">var <\/span>\r\na = intArray[0];\r\n<span style=\"color: blue\">var <\/span>\r\nb = strArray[0];\r\n<span style=\"color: blue\">var <\/span>\r\nc = anonymousTypeArray[1].Name;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\u533f\u540d\u6570\u7ec4\u8c03\u8bd5\" height=\"333\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ5.gif\" width=\"550\" \/><\/p>\n<p><em>\u533f\u540d\u7c7b\u578b\u8981\u70b9<\/em>\n<\/p>\n<ol>\n<li>\u53ef\u4ee5\u4f7f\u7528new\u5173\u952e\u5b57\u8c03\u7528\u533f\u540d\u521d\u59cb\u5316\u5668\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u533f\u540d\u7c7b\u578b\u76f4\u63a5\u7ee7\u627f\u81eaSystem. Object\u3002<\/li>\n<li>\u533f\u540d\u7c7b\u578b\u7684\u6210\u5458\u662f\u7f16\u8bd1\u5668\u6839\u636e\u521d\u59cb\u5316\u5668\u63a8\u65ad\u800c\u6765\u7684\u4e00\u4e9b\u8bfb\u5199\u5c5e\u6027\u3002<\/li>\n<\/ol>\n<h1>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668(Object and Collection Initializers)<\/h1>\n<h3>\u5bf9\u8c61\u521d\u59cb\u5316\u5668 (Object Initializers) \uff1a<\/h3>\n<p>.NET2.0\u6846\u67b6\u4e2d\u7684\u7c7b\u578b\u975e\u5e38\u4f9d\u8d56\u4e8e\u5c5e\u6027\u3002\u5f53\u751f\u6210\u5bf9\u8c61\u5b9e\u4f8b\u548c\u4f7f\u7528\u65b0\u7684\u7c7b\u578b\u65f6\uff0c\u5728.Net2.0\u65f6\u5019\u6211\u4eec\u50cf\u8fd9\u6837\u5199\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">User <\/span>\r\nuser = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User<\/span>\r\n();\r\nuser.Id = 1;\r\nuser.Name = <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n;\r\nuser.Age = 22;<\/pre>\n<p>\u5728VS2008\u4e2d\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5730\u751f\u6210\u5408\u9002\u7684\u5c5e\u6027setter\u4ee3\u7801\uff0c\u4f7f\u5f97\u539f\u6765\u51e0\u884c\u7684\u5c5e\u6027\u8d4b\u503c\u64cd\u4f5c\u53ef\u4ee5\u5728\u4e00\u884c\u5b8c\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u7b80\u5316\uff1a\u50cf\u8fd9\u6837\uff0c<strong>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u7531\u4e00\u7cfb\u5217\u6210\u5458\u5bf9\u8c61\u7ec4\u6210\uff0c\u5176\u5bf9\u8c61\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u7528\u9017\u53f7\u95f4\u9694\uff0c\u4f7f\u7528{}\u5c01\u95ed\u3002<\/strong>\n<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">User <\/span>\r\nuser = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User <\/span>\r\n{ Id = 1, Name = <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n, Age = 22 };<\/pre>\n<p>\u53c8\u4f8b\u5982\uff0c\u6211\u628a\u4e8c\u4e2a\u4eba\u52a0\u5230\u4e00\u4e2a\u57fa\u4e8e\u6cdb\u578b\u7684\u7c7b\u578b\u4e3aUser\u7684List\u96c6\u5408\u4e2d\uff1a<\/p>\n<pre class=\"code\">List&lt;<span style=\"color: #2b91af\">User<\/span>\r\n&gt; user = <span style=\"color: blue\">new <\/span>\r\nList&lt;<span style=\"color: #2b91af\">User<\/span>\r\n&gt;{\r\n    <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User<\/span>\r\n{Id=1,Name=<span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n,Age=22},\r\n    <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User<\/span>\r\n{Id=2,Name=<span style=\"color: #a31515\">&quot;XieQing&quot;<\/span>\r\n,Age=25},\r\n};<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u8c03\u8bd5\" height=\"278\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ6.gif\" width=\"550\" \/><\/p>\n<p>\u5982\u679c\u6709\u76f8\u540c\u540d\u5b57\u548c\u7c7b\u578b\u7684\u4e24\u4e2a\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u5c06\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u76f8\u4e92\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">User <\/span>\r\nuser = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User <\/span>\r\n{ Id = 1, Name = <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n, Age = 22 };\r\n<span style=\"color: #2b91af\">User <\/span>\r\nuser2 = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User <\/span>\r\n{ Id = 2, Name = <span style=\"color: #a31515\">&quot;XieQing&quot;<\/span>\r\n, Age = 25 };\r\nuser = user2; <\/pre>\n<p>\u9664\u4e86\u5728\u521d\u59cb\u5316\u7c7b\u65f6\u8bbe\u7f6e\u7b80\u5355\u7684\u5c5e\u6027\u503c\u5916\uff0c\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u7279\u6027\u4e5f\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u66f4\u590d\u6742\u7684\u5d4c\u5957(nested)\u5c5e\u6027\u7c7b\u578b\u3002\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u5728\u4e0a\u9762\u5b9a\u4e49\u7684User\u7c7b\u578b\u540c\u65f6\u62e5\u6709\u4e00\u4e2a\u5c5e\u4e8eAddress\u7c7b\u578b\u7684\u53eb&ldquo;Address&rdquo;\u7684\u5c5e\u6027\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">User<\/span>\r\n user = <span style=\"color: #0000ff\">new<\/span>\r\n <span style=\"color: #2b91af\">User<\/span>\r\n\r\n{\r\n    Id = 1,\r\n    Name = <span style=\"color: #8b0000\">&quot;YJingLee&quot;<\/span>\r\n,\r\n    Age = 22,\r\n    <span style=\"color: #2b91af\">Address<\/span>\r\n = <span style=\"color: #0000ff\">new<\/span>\r\n <span style=\"color: #2b91af\">Address<\/span>\r\n\r\n    {\r\n        City = <span style=\"color: #8b0000\">&quot;NanJing&quot;<\/span>\r\n,\r\n        Zip = 21000\r\n    }\r\n};<\/pre>\n<h3>\u96c6\u5408\u521d\u59cb\u5316\u5668(Collection Initializers)\uff1a<\/h3>\n<p><strong>\u96c6\u5408\u521d\u59cb\u5316\u5668\u7531\u4e00\u7cfb\u5217\u96c6\u5408\u5bf9\u8c61\u7ec4\u6210\uff0c\u7528\u9017\u53f7\u95f4\u9694\uff0c\u4f7f\u7528{}\u5c01\u95ed\u3002<\/strong><br \/>\n<br \/>\u96c6\u5408\u521d\u59cb\u5316\u5668\u53ef\u4ee5\u7b80\u5316\u628a\u51e0\u4e2a\u5bf9\u8c61\u4e00\u8d77\u6dfb\u52a0\u5230\u4e00\u4e2a\u96c6\u5408\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4e3a\u4f60\u505a\u96c6\u5408\u63d2\u5165\u64cd\u4f5c\u3002\u4f8b\u5982\u6211\u628a\u4e03\u4e2a\u6570\u52a0\u5230\u4e00\u4e2a\u57fa\u4e8e\u6cdb\u578b\u7684\u7c7b\u578b\u4e3aint\u7684List\u96c6\u5408\u4e2d<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">List<\/span>\r\n&lt;<span style=\"color: blue\">int<\/span>\r\n&gt; num = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">List<\/span>\r\n&lt;<span style=\"color: blue\">int<\/span>\r\n&gt; { 0, 1, 2, 6, 7, 8, 9 };<\/pre>\n<p><em>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668\u8981\u70b9<\/em>\n<\/p>\n<ol>\n<li>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u5b9e\u9645\u4e0a\u5229\u7528\u4e86\u7f16\u8bd1\u5668\u5bf9\u5bf9\u8c61\u4e2d\u5bf9\u5916\u53ef\u89c1\u7684\u5b57\u6bb5\u548c\u5c5e\u6027\u8fdb\u884c\u6309\u5e8f\u8d4b\u503c\u3002<\/li>\n<li>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u5141\u8bb8\u53ea\u7ed9\u4e00\u90e8\u5206\u5c5e\u6027\u8d4b\u503c\uff0c\u5305\u62ecinternal\u8bbf\u95ee\u7ea7\u522b<\/li>\n<li>\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u53ef\u4ee5\u7ed3\u5408\u6784\u9020\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u5e76\u4e14\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5148\u4e8e\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u6267\u884c\u3002<\/li>\n<li>\u96c6\u5408\u521d\u59cb\u5316\u5668\u4f1a\u5bf9\u521d\u59cb\u5316\u5668\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6309\u5e8f\u8c03\u7528ICollection&lt;T&gt;.Add(T)\u65b9\u6cd5\u3002<\/li>\n<li>\u6ce8\u610f\u5bf9\u8c61\u521d\u59cb\u5316\u5668\u548c\u96c6\u5408\u521d\u59cb\u5316\u5668\u4e2d\u6210\u5458\u7684\u53ef\u89c1\u6027\u548c\u8c03\u7528\u987a\u5e8f\u3002<\/li>\n<li>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668\u540c\u6837\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u6280\u672f\u3002<\/li>\n<\/ol>\n<p>C# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u5c5e\u6027(Auto-Implemented Properties)<\/li>\n<li>\u9690\u542b\u7c7b\u578b\u5c40\u90e8\u53d8\u91cf(Local Variable Type Inference)<\/li>\n<li>\u533f\u540d\u7c7b\u578b(Anonymous Types)<\/li>\n<li>\u5bf9\u8c61\u4e0e\u96c6\u5408\u521d\u59cb\u5316\u5668(Object and Collection Initializers)<\/li>\n<li>\u6269\u5c55\u65b9\u6cd5(Extension Methods)<\/li>\n<li>Lambda\u8868\u8fbe\u5f0f\u548cLambda\u8868\u8fbe\u5f0f\u6811 (Lambda Expression and Lambda Expression Trees)<\/li>\n<\/ul>\n<h1>\u6269\u5c55\u65b9\u6cd5(Extension Methods)<\/h1>\n<p>\u5f80\u5f80\u6211\u4eec\u9700\u8981\u5bf9CLR\u7c7b\u578b\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4f46\u82e6\u4e8e\u65e0\u6cd5\u6269\u5c55CLR\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u53ea\u80fd\u521b\u5efa\u4e00\u4e9bhelper\u65b9\u6cd5\uff0c\u6216\u8005\u7ee7\u627f\u7c7b\u3002\u6211\u4eec\u6765\u4fee\u6539\u4e0a\u9762\u7684User\u7c7b\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">public class <\/span>\r\n<span style=\"color: #2b91af\">User\r\n<\/span>\r\n{\r\n    <span style=\"color: blue\">public int <\/span>\r\nId { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public string <\/span>\r\nName { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public int <\/span>\r\nAge { <span style=\"color: blue\">get<\/span>\r\n; <span style=\"color: blue\">set<\/span>\r\n; }\r\n    <span style=\"color: blue\">public string <\/span>\r\nRead()\r\n    {\r\n        <span style=\"color: blue\">return <\/span>\r\n<span style=\"color: #a31515\">&quot;Id:&quot; <\/span>\r\n+ Id + <span style=\"color: #a31515\">&quot;\u59d3\u540d:&quot; <\/span>\r\n+ Name + <span style=\"color: #a31515\">&quot;\u5e74\u9f84:&quot; <\/span>\r\n+ Age;\r\n    }\r\n}<\/pre>\n<p>\u7136\u540e\u8c03\u7528<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\nuser = <span style=\"color: blue\">new <\/span>\r\n{ Id = 1, Name = <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n, Age = 22 };\r\n<span style=\"color: blue\">var <\/span>\r\nstr = user.Read();<\/pre>\n<p>\u73b0\u5728\u6709\u4e86\u6269\u5c55\u65b9\u6cd5\u5c31\u65b9\u4fbf\u591a\u4e86\u3002<\/p>\n<p>\u6269\u5c55\u65b9\u6cd5\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5f80\u4e00\u4e2a\u73b0\u6709\u7684CLR\u7c7b\u578b\u7684\u516c\u5f00\u5951\u7ea6(contract)\u4e2d\u6dfb\u52a0\u65b0\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u7528\u751f\u6210\u5b50\u7c7b\u6216\u8005\u91cd\u65b0\u7f16\u8bd1\u539f\u6765\u7684\u7c7b\u578b\u3002\u6269\u5c55\u65b9\u6cd5\u6709\u52a9\u4e8e\u628a\u4eca\u5929\u52a8\u6001\u8bed\u8a00\u4e2d\u6d41\u884c\u7684\u5bf9duck typing\u7684\u652f\u6301\u4e4b\u7075\u6d3b\u6027\uff0c\u4e0e\u5f3a\u7c7b\u578b\u8bed\u8a00\u4e4b\u6027\u80fd\u548c\u7f16\u8bd1\u65f6\u9a8c\u8bc1\u878d\u5408\u8d77\u6765\u3002&mdash;&mdash;<em>\u5f15\u7528Scott\u535a\u6587<\/em>\n<\/p>\n<p>\u6269\u5c55\u65b9\u6cd5\u662f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b9e\u4f8b\u65b9\u6cd5\u8bed\u6cd5\u8c03\u7528\u7684\u9759\u6001\u65b9\u6cd5\u3002\u6548\u679c\u4e0a\uff0c\u4f7f\u5f97\u9644\u52a0\u7684\u65b9\u6cd5\u6269\u5c55\u5df2\u5b58\u5728\u7c7b\u578b\u548c\u6784\u9020\u7c7b\u578b\u6210\u4e3a\u53ef\u80fd\u3002\u4ed6\u53ef\u4ee5\u5bf9\u73b0\u6709\u7c7b\u529f\u80fd\u8fdb\u884c\u6269\u5145\uff0c\u4ece\u800c\u4f7f\u8be5\u7c7b\u578b\u7684\u5b9e\u4f8b\u5177\u6709\u66f4\u591a\u7684\u65b9\u6cd5\uff08\u529f\u80fd\uff09\u3002<br \/>\u6269\u5c55\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6539\u53d8\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6269\u5c55\uff08\u5373\u6dfb\u52a0\u4e0d\u80fd\u4fee\u6539\uff09\u73b0\u6709\u7c7b\u578b\u4e2d\u7684\u5b9e\u4f8b\u65b9\u6cd5\u3002<\/p>\n<p>\u6269\u5c55\u65b9\u6cd5\u7ed9\u6211\u4eec\u4e00\u4e2a\u600e\u6837\u7684\u601d\u8def\u5462\uff1f\u6211\u4eec\u4e00\u6b65\u4e00\u6b65\u505a\u4e00\u4e0b\uff01<br \/>\u9996\u5148\u58f0\u660e\u6269\u5c55\u65b9\u6cd5\uff1a\u901a\u8fc7\u6307\u5b9a\u5173\u952e\u5b57this\u4fee\u9970\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u6ce8\u610f\u6269\u5c55\u65b9\u6cd5\u4ec5\u53ef\u58f0\u660e\u5728\u9759\u6001\u7c7b\u4e2d\u3002\u6269\u5c55\u65b9\u6cd5\u5177\u5907\u6240\u6709\u5e38\u89c4\u9759\u6001\u65b9\u6cd5\u7684\u6240\u6709\u80fd\u529b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b9e\u4f8b\u65b9\u6cd5\u8bed\u6cd5\u6765\u8c03\u7528\u3002\u63a5\u7740\u5c31\u53ef\u4ee5\u8c03\u7528\u6269\u5c55\u65b9\u6cd5\u4e86\u3002\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u5b9e\u4f8b\u5206\u6790\u4e00\u4e0b\uff1a<br \/>\u4f8b\u5982\u6211\u4eec\u8981\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf\u662f\u5426\u662f\u5408\u6cd5\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1f\u5728.Net2.0\u6846\u67b6\u4e0b\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\nemail = <span style=\"color: #a31515\">&quot;leeyongjing@gmail.com&quot;<\/span>\r\n;\r\n<span style=\"color: blue\">if <\/span>\r\n(EmailValidator.IsValid(email))\r\n{\r\n    Response.Write(<span style=\"color: #a31515\">&quot;YJingLee\u63d0\u793a:\u8fd9\u662f\u4e00\u4e2a\u6b63\u786e\u7684\u90ae\u4ef6\u5730\u5740&quot;<\/span>\r\n);\r\n}<\/pre>\n<p>\u800c\u4f7f\u7528\u6269\u5c55\u65b9\u6cd5\u7684\u8bdd\uff0c\u6211\u53ef\u4ee5\u6dfb\u52a0&ldquo;IsValidEmailAddress()&rdquo;\u65b9\u6cd5\u5230string\u7c7b\u672c\u8eab\u4e2d\u53bb\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u5b57\u7b26\u4e32\u5b9e\u4f8b\u662f\u5426\u662f\u4e2a\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"code\"><span style=\"color: blue\">if <\/span>\r\n(email.IsValidEmailAddress())\r\n{\r\n    Response.Write(<span style=\"color: #a31515\">&quot;YJingLee\u63d0\u793a:\u8fd9\u662f\u4e00\u4e2a\u6b63\u786e\u7684\u90ae\u4ef6\u5730\u5740&quot;<\/span>\r\n);\r\n}<\/pre>\n<p>\u6211\u4eec\u662f\u600e\u4e48\u628a\u8fd9\u4e2aIsValidEmailAddress()\u65b9\u6cd5\u6dfb\u52a0\u5230\u73b0\u6709\u7684string\u7c7b\u91cc\u53bb\u7684\u5462\uff1f\u5148\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u7c7b\uff0c\u518d\u5b9a\u4e49&ldquo;IsValidEmailAddress&rdquo;\u8fd9\u4e2a\u9759\u6001\u7684\u6cd5\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<pre class=\"code\"><span style=\"color: blue\">public static class <\/span>\r\n<span style=\"color: #2b91af\">Extensions<\/span>\r\n<span style=\"color: green\">\/\/\u9759\u6001\u7c7b\r\n<\/span>\r\n{\r\n    <span style=\"color: blue\">public static bool <\/span>\r\nIsValidEmailAddress(<span style=\"color: blue\">this string <\/span>\r\ns)\r\n    <span style=\"color: green\">\/\/\u9759\u6001\u65b9\u6cd5\u548cthis\r\n    <\/span>\r\n{\r\n        <span style=\"color: #2b91af\">Regex <\/span>\r\nregex = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">Regex<\/span>\r\n(<span style=\"color: #a31515\">@&quot;^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$&quot;<\/span>\r\n);\r\n        <span style=\"color: blue\">return <\/span>\r\nregex.IsMatch(s);\r\n    }\r\n}<\/pre>\n<p>\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u9759\u6001\u65b9\u6cd5\u5728\u7b2c\u4e00\u4e2a\u7c7b\u578b\u662fstring\u7684\u53c2\u6570\u53d8\u91cf\u524d\u6709\u4e2a&ldquo;this&rdquo;\u5173\u952e\u8bcd\uff0c\u8fd9\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u4e2a\u7279\u5b9a\u7684\u6269\u5c55\u65b9\u6cd5\u5e94\u8be5\u6dfb\u52a0\u5230\u7c7b\u578b\u4e3a&ldquo;string&rdquo;\u7684\u5bf9\u8c61\u4e2d\u53bb\u3002\u7136\u540e\u5728IsValidEmailAddress()\u65b9\u6cd5\u5b9e\u73b0\u91cc\uff0c\u6211\u53ef\u4ee5\u8bbf\u95ee\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5b9e\u9645string\u5b9e\u4f8b\u7684\u6240\u6709\u516c\u5f00\u5c5e\u6027\/\u65b9\u6cd5\/\u4e8b\u4ef6\uff0c\u53d6\u51b3\u4e8e\u5b83\u662f\u5426\u662f\u5408\u6cd5\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u6765\u8fd4\u56detrue\/false\u3002<\/p>\n<p>\u6269\u5c55\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u5e94\u7528\u5230\u4e2a\u522b\u7c7b\u578b\u4e0a\uff0c\u4e5f\u80fd\u5e94\u7528\u5230.NET\u6846\u67b6\u4e2d\u4efb\u4f55\u57fa\u7c7b\u6216\u63a5\u53e3\u4e0a\u3002\u5373\u53ef\u7528\u4e8e\u6574\u4e2a.NET\u6846\u67b6\u4e30\u5bcc\u7684\u53ef\u7ec4\u5408\u7684\u6846\u67b6\u5c42\u6269\u5c55\u3002<\/p>\n<h3>\u6269\u5c55\u65b9\u6cd5\u8981\u70b9<\/h3>\n<ol>\n<li>\u6269\u5c55\u65b9\u6cd5\u7684\u672c\u8d28\u4e3a\u5c06\u5b9e\u4f8b\u65b9\u6cd5\u8c03\u7528\u5728\u7f16\u8bd1\u671f\u6539\u53d8\u4e3a\u9759\u6001\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5b83\u786e\u5b9e\u62e5\u6709\u9759\u6001\u65b9\u6cd5\u6240\u5177\u6709\u7684\u6240\u6709\u529f\u80fd\u3002<\/li>\n<li>\u6269\u5c55\u65b9\u6cd5\u7684\u4f5c\u7528\u57df\u662f\u6574\u4e2anamespace\u53ef\u89c1\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7using namespace\u6765\u5bfc\u5165\u5176\u5b83\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6269\u5c55\u65b9\u6cd5\u3002<br \/>&nbsp;<\/li>\n<li>\u6269\u5c55\u65b9\u6cd5\u7684\u4f18\u5148\u7ea7\uff1a\u73b0\u6709\u5b9e\u4f8b\u65b9\u6cd5\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u5176\u6b21\u4e3a\u6700\u8fd1\u7684namespace\u4e0b\u7684\u9759\u6001\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u6700\u540e\u4e3a\u8f83\u8fdc\u7684namespace\u4e0b\u7684\u9759\u6001\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u3002<br \/>&nbsp;<\/li>\n<li>\u6269\u5c55\u65b9\u6cd5\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u6280\u672f\uff0c\u6ce8\u610f\u4e0e\u53cd\u5c04\u7b49\u8fd0\u884c\u65f6\u6280\u672f\u8fdb\u884c\u533a\u522b\uff0c\u5e76\u614e\u91cd\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<h1>Lambda\u8868\u8fbe\u5f0f\u548cLambda\u8868\u8fbe\u5f0f\u6811 (Lambda Expression and Lambda Expression Trees)<\/h1>\n<h3>Lambda\u8868\u8fbe\u5f0f<\/h3>\n<p>\u6211\u4eec\u4ece&ldquo;\u6240\u6709\u5b57\u7b26\u4e32\u67e5\u627e\u5305\u542bYJingLee\u5b50\u5b57\u7b26\u4e32&rdquo;\u8bf4\u8d77\u3002\u5728C# 2.0\u4e2d\uff0c\u533f\u540d\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u4ee5\u5185\u8054\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u59d4\u6258\u5b9e\u4f8b\uff0c\u5b83\u63d0\u4f9b\u5f3a\u5927\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u662f\u6807\u8bb0\u663e\u5f97\u76f8\u5f53\u7684\u5197\u957f\u548c\u5e26\u6709\u5f3a\u5236\u6027\u3002\u6211\u4eec\u4f7f\u7528C# 2.0 \u4e2d\u7684\u533f\u540d\u65b9\u6cd5\u67e5\u627e\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\ninString = list.FindAll(<span style=\"color: blue\">delegate<\/span>\r\n(<span style=\"color: blue\">string <\/span>\r\ns)\r\n{ <span style=\"color: blue\">return <\/span>\r\ns.Indexof(<span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n) &gt;= 0; });<\/pre>\n<p>\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528C# 3.0\u5e26\u6765\u7684Lambda\u8868\u8fbe\u5f0f\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4e00\u79cd\u66f4\u63a5\u8fd1\u4eba\u7684\u601d\u7ef4\u3001\u66f4\u81ea\u7136\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7c7b\u4f3c\u4e8e\u533f\u540d\u65b9\u6cd5\u540c\u6837\u7684\u6548\u679c\uff0c\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u591a\u4e48\u7b80\u6d01\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\ninString = list.FindAll(s =&gt; s.Indexof(<span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n) &gt;= 0);<\/pre>\n<p>Lambda\u8868\u8fbe\u5f0f\u683c\u5f0f\uff1a(\u53c2\u6570\u5217\u8868)=&gt;\u8868\u8fbe\u5f0f\u6216\u8bed\u53e5\u5757<br \/>\u5177\u4f53\u610f\u4e49\uff1a\u5b9a\u4e49Lambda\u63a5\u53d7\u53c2\u6570\u5217\u8868\uff0c\u8fd0\u884c\u8868\u8fbe\u5f0f\u6216\u8bed\u53e5\u5757\u8fd4\u56de\u8868\u8fbe\u5f0f\u6216\u8bed\u53e5\u5757\u7684\u503c\u4f20\u7ed9\u8fd9\u4e2a\u53c2\u6570\u5217\u8868\u3002<\/p>\n<p>Lambda\u8868\u8fbe\u5f0f\u53c2\u6570\u7c7b\u578b\u53ef\u4ee5\u662f\u9690\u5f0f\u7c7b\u578b\u6216\u663e\u5f0f\u7c7b\u578b\u3002\u5728\u663e\u5f0f\u5217\u8868\u4e2d\uff0c\u6bcf\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u662f\u663e\u5f0f\u6307\u5b9a\u7684\uff0c\u5728\u9690\u5f0f\u5217\u8868\u4e2d\uff0c\u53c2\u6570\u7684\u7c7b\u578b\u7531Lambda\u8868\u8fbe\u5f0f\u51fa\u73b0\u7684\u8bed\u5883\u81ea\u52a8\u63a8\u65ad\u7c7b\u578b\u3002<br \/>Lambda\u8868\u8fbe\u5f0f\u7684\u53c2\u6570\u5217\u8868\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\uff0c\u6216\u8005\u65e0\u53c2\u6570\u3002\u5728\u6709\u5355\u4e00\u7684\u9690\u578b\u53c2\u6570\u7684lambda\u8868\u8fbe\u5f0f\u4e2d\uff0c\u5706\u62ec\u53f7\u53ef\u4ee5\u4ece\u53c2\u6570\u5217\u8868\u4e2d\u7701\u7565\u3002<br \/>\u4f8b\u5982\uff1a<\/p>\n<pre class=\"code\">(x, y) =&gt; x * y;<span style=\"color: green\">\/\/\u591a\u53c2\u6570\uff0c\u9690\u5f0f\u7c7b\u578b=&gt;\u8868\u8fbe\u5f0f\r\n<\/span>\r\nx =&gt; x * 10;<span style=\"color: green\">\/\/\u5355\u53c2\u6570\uff0c\u9690\u5f0f\u7c7b\u578b=&gt;\u8868\u8fbe\u5f0f\r\n<\/span>\r\nx =&gt; { <span style=\"color: blue\">return <\/span>\r\nx * 10; }; <span style=\"color: green\">\/\/\u5355\u53c2\u6570\uff0c\u9690\u5f0f\u7c7b\u578b=&gt;\u8bed\u53e5\u5757\r\n<\/span>\r\n(<span style=\"color: blue\">int <\/span>\r\nx) =&gt; x * 10;<span style=\"color: green\">\/\/\u5355\u53c2\u6570\uff0c\u663e\u5f0f\u7c7b\u578b=&gt;\u8868\u8fbe\u5f0f\r\n<\/span>\r\n(<span style=\"color: blue\">int <\/span>\r\nx) =&gt; { <span style=\"color: blue\">return <\/span>\r\nx * 10; };<span style=\"color: green\">\/\/\u5355\u53c2\u6570\uff0c\u663e\u5f0f\u7c7b\u578b=&gt;\u8bed\u53e5\u5757\r\n<\/span>\r\n() =&gt; <span style=\"color: #2b91af\">Console<\/span>\r\n.WriteLine(); <span style=\"color: green\">\/\/\u65e0\u53c2\u6570<\/span>\r\n<\/pre>\n<p>\u4e0b\u9762\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a<br \/>\u5728\u524d\u9762\u7684\u5e16\u5b50\u4e2d\uff0c\u6211\u4eec\u5199\u4e86\u4e00\u4e2aUser\u7c7b\u53ca\u589e\u52a0\u4e862\u4e2a\u4eba\uff0c\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u7531LINQ\u63d0\u4f9b\u7684\u65b0\u7684Where\u548cAverage\u65b9\u6cd5\u6765\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u4eba\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u4ee5\u53ca\u8ba1\u7b97\u8fd9\u4e2a\u96c6\u5408\u4e2d\u7684\u4eba\u7684\u5e73\u5747\u5e74\u9f84\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">List<\/span>\r\n&lt;<span style=\"color: #2b91af\">User<\/span>\r\n&gt; user = <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">List<\/span>\r\n&lt;<span style=\"color: #2b91af\">User<\/span>\r\n&gt;{\r\n    <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User<\/span>\r\n{Id=1,Name=<span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n,Age=22},\r\n    <span style=\"color: blue\">new <\/span>\r\n<span style=\"color: #2b91af\">User<\/span>\r\n{Id=2,Name=<span style=\"color: #a31515\">&quot;XieQing&quot;<\/span>\r\n,Age=25},\r\n};\r\n<span style=\"color: green\">\/\/\u83b7\u53d6\u7279\u5b9a\u4eba\u65f6\u6240\u7528\u7684\u8fc7\u6ee4\u6761\u4ef6\uff0cp\u53c2\u6570\u5c5e\u4e8eUser\u7c7b\u578b\r\n<\/span>\r\n<span style=\"color: blue\">var <\/span>\r\nresults = user.Where(p =&gt; p.Name == <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n).ToList();\r\n<span style=\"color: green\">\/\/\u7528User\u5bf9\u8c61\u7684Age\u503c\u8ba1\u7b97\u5e73\u5747\u5e74\u9f84\r\n<\/span>\r\n<span style=\"color: blue\">var <\/span>\r\naverage = user.Average(p =&gt; p.Age);<\/pre>\n<p>\u6548\u679c\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ7.gif\" target=\"_blank\"><img decoding=\"async\" alt=\"Lambda\u8868\u8fbe\u5f0f\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ7.gif\" \/><\/a>\n<\/p>\n<p>\u5bf9\u8fd9\u4e2aLambda\u8868\u8fbe\u5f0f\u505a\u4e2a\u7b80\u8981\u5206\u6790\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: blue\">var <\/span>\r\nresultsdelegate = user.Where(<span style=\"color: blue\">delegate<\/span>\r\n(<span style=\"color: #2b91af\">User <\/span>\r\np)\r\n{\r\n    <span style=\"color: blue\">return <\/span>\r\np.Name == <span style=\"color: #a31515\">&quot;YJingLee&quot;<\/span>\r\n;<span style=\"color: green\">\/\/ \u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\r\n<\/span>\r\n});\r\n<span style=\"color: blue\">var <\/span>\r\naveragedelegate = user.Average(<span style=\"color: blue\">delegate<\/span>\r\n(<span style=\"color: #2b91af\">User <\/span>\r\np)\r\n{\r\n    <span style=\"color: blue\">return <\/span>\r\np.Age;\r\n});<\/pre>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ8.gif\" target=\"_blank\"><img decoding=\"async\" alt=\"Lambda\u8868\u8fbe\u5f0f\u7b80\u8981\u5206\u6790\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ8.gif\" \/><\/a>\n<\/p>\n<p>Lambda\u8868\u8fbe\u5f0fL\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u59d4\u6258\u7c7b\u578bD\uff0c\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<br \/>L\u7684\u53c2\u6570\u7c7b\u578b\u8981\u4e0eD\u7684\u53c2\u6570\u4e2a\u6570\u76f8\u7b49\uff0c\u7c7b\u578b\u76f8\u540c\uff0c\u8fd4\u56de\u7c7b\u578b\u76f8\u540c\uff0c\u65e0\u8bba\u662f\u8868\u8fbe\u5f0f\uff0c\u8fd8\u662f\u8bed\u53e5\u5757\u3002\u6ce8\u610f\u9690\u5f0f\u7c7b\u578b\u8981\u53c2\u4e0e\u7c7b\u578b\u8fa8\u6790\u3002<\/p>\n<h3>Lambda\u8868\u8fbe\u5f0f\u6811<\/h3>\n<p>Lambda\u8868\u8fbe\u5f0f\u6811\u5141\u8bb8\u6211\u4eec\u50cf\u5904\u7406\u6570\u636e\uff08\u6bd4\u5982\u8bfb\u53d6\uff0c\u4fee\u6539\uff09\u4e00\u6837\u6765\u5904\u7406Lambda\u8868\u8fbe\u5f0f\u3002\u6211\u4ee5\u4e00\u4e2a\u4f8b\u5b50\u7b80\u5355\u8bf4\u660e\uff1a<\/p>\n<pre class=\"code\"><span style=\"color: #2b91af\">Expression<\/span>\r\n&lt;<span style=\"color: #2b91af\">Func<\/span>\r\n&lt;<span style=\"color: blue\">int<\/span>\r\n, <span style=\"color: blue\">bool<\/span>\r\n&gt;&gt; filter = n =&gt; (n * 3) &lt; 5;\r\n<span style=\"color: #2b91af\">BinaryExpression <\/span>\r\nlt = (<span style=\"color: #2b91af\">BinaryExpression<\/span>\r\n)filter.Body;\r\n<span style=\"color: #2b91af\">BinaryExpression <\/span>\r\nmult = (<span style=\"color: #2b91af\">BinaryExpression<\/span>\r\n)lt.Left;\r\n<span style=\"color: #2b91af\">ParameterExpression <\/span>\r\nen = (<span style=\"color: #2b91af\">ParameterExpression<\/span>\r\n)mult.Left;\r\n<span style=\"color: #2b91af\">ConstantExpression <\/span>\r\nthree = (<span style=\"color: #2b91af\">ConstantExpression<\/span>\r\n)mult.Right;\r\n<span style=\"color: #2b91af\">ConstantExpression <\/span>\r\nfive = (<span style=\"color: #2b91af\">ConstantExpression<\/span>\r\n)lt.Right;\r\n<span style=\"color: blue\">var <\/span>\r\nOne = filter.Compile();\r\n<span style=\"color: #2b91af\">Console<\/span>\r\n.WriteLine(<span style=\"color: #a31515\">&quot;Result: {0},{1}&quot;<\/span>\r\n, One(5), One(1));\r\n<span style=\"color: #2b91af\">Console<\/span>\r\n.WriteLine(<span style=\"color: #a31515\">&quot;({0} ({1} {2} {3}) {4})&quot;<\/span>\r\n, lt.NodeType,\r\n         mult.NodeType, en.Name, three.Value, five.Value);<\/pre>\n<p>\u6548\u679c\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><a href=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ9.gif\" target=\"_blank\"><img decoding=\"async\" alt=\"Lambda\u8868\u8fbe\u5f0f\u6811\" src=\"http:\/\/lyj.cnblogs.com\/images\/cnblogs_com\/lyj\/LINQ\/LINQ9.gif\" \/><\/a>\n<\/p>\n<h3>Lambda\u8868\u8fbe\u5f0f\u548cLambda\u8868\u8fbe\u5f0f\u6811\u8981\u70b9<\/h3>\n<ol>\n<li>Lambda\u8868\u8fbe\u5f0f\u7684\u53c2\u6570\u7c7b\u578b\u53ef\u4ee5\u5ffd\u7565\uff0c\u56e0\u4e3a\u53ef\u4ee5\u6839\u636e\u4f7f\u7528\u7684\u4e0a\u4e0b\u6587\u8fdb\u884c\u63a8\u65ad\u3002<\/li>\n<li>Lambda\u8868\u8fbe\u5f0f\u7684\u4e3b\u4f53(body)\u53ef\u4ee5\u662f\u8868\u8fbe\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u662f\u8bed\u53e5\u5757\u3002<\/li>\n<li>Lambda\u8868\u8fbe\u5f0f\u4f20\u5165\u7684\u5b9e\u53c2\u5c06\u53c2\u4e0e\u7c7b\u578b\u63a8\u65ad\uff0c\u4ee5\u53ca\u65b9\u6cd5\u91cd\u8f7d\u8fa8\u6790\u3002<\/li>\n<li>Lambda\u8868\u8fbe\u5f0f\u548c\u8868\u8fbe\u5f0f\u4f53\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u8868\u8fbe\u5f0f\u6811\u3002<\/li>\n<li>\u8868\u8fbe\u5f0f\u6811\u5141\u8bb8lambda\u8868\u8fbe\u5f0f\u80fd\u591f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\u66ff\u4ee3\u8868\u793a\u4e3a\u6267\u884c\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p>\u597d\u4e86\uff0c\u6211\u5728\u8fd9\u91cc\u7b80\u5355\u7684\u628aC# 3.0\u65b0\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb\u8bf4\u4e86\u4e00\u4e0b\uff0c\u63a5\u4e0b\u6765\uff0c\u6b63\u5f0f\u8fdb\u5165\u8fd9\u4e2a\u7cfb\u5217\u7684\u4e3b\u9898\u90e8\u5206&mdash;&mdash;LINQ\u3002\u4e3a \u4e86\u8ba9\u5927\u5bb6\u4e86\u89e3\uff0c\u6211\u6362\u4e00\u79cd\u624b\u6cd5\u6765\u5199\uff0c\u4ece\u4e00\u6761\u4e00\u6761LINQ to SQL\u8bed\u53e5\u5206\u6790\u6765\u8d2f\u7a7fLINQ\u7684\u77e5\u8bc6\u70b9\u3002\u4e00\u8d77\u4f53\u9a8cLINQ\u5e26\u7ed9\u6211\u4eec\u7684\u4e50\u8da3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u603b\u4f53\u6765\u8bf4\uff0cVisual Studio 2008\u548c.NET 3.5\u662f\u5efa\u7acb\u5728.NET &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/97\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[42],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-article","tag-LINQ"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}