Finished Creating a Custom Filter Operator
parent
ea26766337
commit
512d8304e7
|
@ -0,0 +1,9 @@
|
||||||
|
namespace Queries
|
||||||
|
{
|
||||||
|
public class Movie
|
||||||
|
{
|
||||||
|
public string Title { get; set; }
|
||||||
|
public float Rating { get; set; }
|
||||||
|
public int Year { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<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
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Introduction", "Introductio
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features", "Features\Features.csproj", "{94C9EE4C-0609-464B-9BFF-BA3EED2A7263}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features", "Features\Features.csproj", "{94C9EE4C-0609-464B-9BFF-BA3EED2A7263}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queries", "Queries\Queries.csproj", "{FB21B6B5-70C3-444E-A1D1-2CCD3F575DD9}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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|x64.Build.0 = Release|Any CPU
|
||||||
{94C9EE4C-0609-464B-9BFF-BA3EED2A7263}.Release|x86.ActiveCfg = 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
|
{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
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
Loading…
Reference in New Issue