From 46766de185b550762cc9e62ab09edc3797306520 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Thu, 25 Feb 2021 13:31:24 +1100 Subject: [PATCH] Customer Filter Operator w/o deferred execution --- Queries/Movie.cs | 17 +++++++++++++++-- Queries/{MyLInq.cs => MyLinq.cs} | 2 +- Queries/Program.cs | 4 ++++ linq-fundamentals.sln | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) rename Queries/{MyLInq.cs => MyLinq.cs} (95%) diff --git a/Queries/Movie.cs b/Queries/Movie.cs index 4fa2432..0728ef3 100644 --- a/Queries/Movie.cs +++ b/Queries/Movie.cs @@ -1,9 +1,22 @@ -namespace Queries +using System; + +namespace Queries { public class Movie { public string Title { get; set; } public float Rating { get; set; } - public int Year { get; set; } + private int _year; + public int Year { + get + { + Console.WriteLine($"Returning {_year} for {Title}"); + return _year; + } + set + { + _year = value; + } + } } } \ No newline at end of file diff --git a/Queries/MyLInq.cs b/Queries/MyLinq.cs similarity index 95% rename from Queries/MyLInq.cs rename to Queries/MyLinq.cs index e6b8b32..efb2118 100644 --- a/Queries/MyLInq.cs +++ b/Queries/MyLinq.cs @@ -5,7 +5,7 @@ using System.Reflection; namespace Queries { - public static class MyLInq + public static class MyLinq { public static IEnumerable Filter(this IEnumerable source, Func predicate) diff --git a/Queries/Program.cs b/Queries/Program.cs index 9dea817..2f603bb 100644 --- a/Queries/Program.cs +++ b/Queries/Program.cs @@ -17,6 +17,8 @@ namespace Queries }; // filter operator from LINQ + Console.WriteLine(); + Console.WriteLine("Filter operator from LINQ"); var query = movies.Where(m => m.Year > 2000); foreach (var movie in query) { @@ -24,6 +26,8 @@ namespace Queries } // filter operator using customized Filter extension + Console.WriteLine(); + Console.WriteLine("Customerized Filter operator"); var query2 = movies.Filter(m => m.Year > 2000); foreach (var movie in query2) { diff --git a/linq-fundamentals.sln b/linq-fundamentals.sln index f718702..a6585a6 100644 --- a/linq-fundamentals.sln +++ b/linq-fundamentals.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features", "Features\Featur EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queries", "Queries\Queries.csproj", "{FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "yield_test", "yield_test\yield_test.csproj", "{790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,5 +60,17 @@ Global {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Release|x64.Build.0 = Release|Any CPU {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Release|x86.ActiveCfg = Release|Any CPU {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Release|x86.Build.0 = Release|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Debug|x64.ActiveCfg = Debug|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Debug|x64.Build.0 = Debug|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Debug|x86.ActiveCfg = Debug|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Debug|x86.Build.0 = Debug|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Release|Any CPU.Build.0 = Release|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Release|x64.ActiveCfg = Release|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Release|x64.Build.0 = Release|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Release|x86.ActiveCfg = Release|Any CPU + {790E4D64-D90A-4289-9F5D-35B6A2BDBE5B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection EndGlobal