Compare commits

...

2 Commits

Author SHA1 Message Date
jason.zhu 46766de185 Customer Filter Operator w/o deferred execution 2021-02-25 13:31:24 +11:00
jason.zhu 512d8304e7 Finished Creating a Custom Filter Operator 2021-02-24 16:44:23 +11:00
5 changed files with 124 additions and 0 deletions

22
Queries/Movie.cs 100644
View File

@ -0,0 +1,22 @@
using System;
namespace Queries
{
public class Movie
{
public string Title { get; set; }
public float Rating { get; set; }
private int _year;
public int Year {
get
{
Console.WriteLine($"Returning {_year} for {Title}");
return _year;
}
set
{
_year = value;
}
}
}
}

28
Queries/MyLinq.cs 100644
View File

@ -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<T> Filter<T>(this IEnumerable<T> source,
Func<T, bool> predicate)
{
// define a customized filter extension for LINQ
// demonstrate how to create a customized LINQ filter
var result = new List<T>();
foreach (var item in source)
{
if (predicate(item))
{
result.Add(item);
}
}
return result;
}
}
}

38
Queries/Program.cs 100644
View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Queries
{
class Program
{
static void Main(string[] args)
{
var movies = new List<Movie>
{
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
Console.WriteLine();
Console.WriteLine("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
Console.WriteLine();
Console.WriteLine("Customerized Filter operator");
var query2 = movies.Filter(m => m.Year > 2000);
foreach (var movie in query2)
{
Console.WriteLine(movie.Title);
}
}
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -7,6 +7,10 @@ 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
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
@ -44,5 +48,29 @@ 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
{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