diff --git a/Queries/Movie.cs b/Queries/Movie.cs new file mode 100644 index 0000000..4fa2432 --- /dev/null +++ b/Queries/Movie.cs @@ -0,0 +1,9 @@ +namespace Queries +{ + public class Movie + { + public string Title { get; set; } + public float Rating { get; set; } + public int Year { get; set; } + } +} \ No newline at end of file diff --git a/Queries/MyLInq.cs b/Queries/MyLInq.cs new file mode 100644 index 0000000..e6b8b32 --- /dev/null +++ b/Queries/MyLInq.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Dynamic; +using System.Reflection; + +namespace Queries +{ + public static class MyLInq + { + public static IEnumerable Filter(this IEnumerable source, + Func predicate) + { + // define a customized filter extension for LINQ + // demonstrate how to create a customized LINQ filter + var result = new List(); + + foreach (var item in source) + { + if (predicate(item)) + { + result.Add(item); + } + } + + return result; + } + } +} \ No newline at end of file diff --git a/Queries/Program.cs b/Queries/Program.cs new file mode 100644 index 0000000..9dea817 --- /dev/null +++ b/Queries/Program.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Queries +{ + class Program + { + static void Main(string[] args) + { + var movies = new List + { + new Movie {Title = "The Dark Knight", Rating = 8.9f, Year = 2008}, + new Movie {Title = "The King's Speech", Rating = 8.0f, Year = 2010}, + new Movie {Title = "Casablanca", Rating = 8.5f, Year = 1942}, + new Movie {Title = "Star Wars V", Rating = 8.5f, Year = 1980} + }; + + // filter operator from LINQ + var query = movies.Where(m => m.Year > 2000); + foreach (var movie in query) + { + Console.WriteLine(movie.Title); + } + + // filter operator using customized Filter extension + var query2 = movies.Filter(m => m.Year > 2000); + foreach (var movie in query2) + { + Console.WriteLine(movie.Title); + } + } + } +} \ No newline at end of file diff --git a/Queries/Queries.csproj b/Queries/Queries.csproj new file mode 100644 index 0000000..2c0dacc --- /dev/null +++ b/Queries/Queries.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/linq-fundamentals.sln b/linq-fundamentals.sln index 564275a..f718702 100644 --- a/linq-fundamentals.sln +++ b/linq-fundamentals.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Introduction", "Introductio EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features", "Features\Features.csproj", "{94C9EE4C-0609-464B-9BFF-BA3EED2A7263}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queries", "Queries\Queries.csproj", "{FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,5 +46,17 @@ Global {94C9EE4C-0609-464B-9BFF-BA3EED2A7263}.Release|x64.Build.0 = Release|Any CPU {94C9EE4C-0609-464B-9BFF-BA3EED2A7263}.Release|x86.ActiveCfg = Release|Any CPU {94C9EE4C-0609-464B-9BFF-BA3EED2A7263}.Release|x86.Build.0 = Release|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Debug|x64.ActiveCfg = Debug|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Debug|x64.Build.0 = Debug|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Debug|x86.ActiveCfg = Debug|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Debug|x86.Build.0 = Debug|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Release|Any CPU.Build.0 = Release|Any CPU + {FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}.Release|x64.ActiveCfg = Release|Any CPU + {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 EndGlobalSection EndGlobal