├── .gitignore ├── Directory.Build.props ├── LICENSE ├── LinqConditionalExtensions.sln ├── README.md ├── logo.png ├── samples └── SampleApp │ ├── Program.cs │ └── SampleApp.csproj └── src ├── Directory.Build.props └── LinqConditionalExtensions ├── ConditionalEnumerableExtensions.cs ├── ConditionalQueryableExtensions.cs ├── EnumerableExtensions.cs ├── IConditionalEnumerable`1.cs ├── IConditionalEnumerable`2.cs ├── IConditionalQueryable`1.cs ├── IConditionalQueryable`2.cs ├── ISwitchableEnumerable`2.cs ├── ISwitchableEnumerable`3.cs ├── ISwitchableQueryable`2.cs ├── ISwitchableQueryable`3.cs ├── Internal ├── ConditionalEnumerable`1.cs ├── ConditionalEnumerable`2.cs ├── ConditionalQueryable`1.cs ├── ConditionalQueryable`2.cs ├── SwitchableEnumerable`2.cs ├── SwitchableEnumerable`3.cs ├── SwitchableQueryable`2.cs └── SwitchableQueryable`3.cs ├── LinqConditionalExtensions.csproj ├── LinqConditionalExtensions.xml ├── QueryableExtensions.cs ├── SwitchableEnumerableExtensions.cs └── SwitchableQueryableExtensions.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | latest 4 | 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Todd Skelton 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LinqConditionalExtensions.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.168 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{6FA8B159-AC0A-4680-9D1D-3AB55D63CC83}" 7 | ProjectSection(SolutionItems) = preProject 8 | Directory.Build.props = Directory.Build.props 9 | EndProjectSection 10 | EndProject 11 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{B80E60CD-2BAE-4905-B305-EB5A1EC5199E}" 12 | EndProject 13 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{81766A62-D89D-40A4-8F2F-2BC0ADAEE204}" 14 | ProjectSection(SolutionItems) = preProject 15 | src\Directory.Build.props = src\Directory.Build.props 16 | EndProjectSection 17 | EndProject 18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqConditionalExtensions", "src\LinqConditionalExtensions\LinqConditionalExtensions.csproj", "{4A42EACD-20CE-4B0E-92C7-379275A43FD3}" 19 | EndProject 20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{99D5B6F9-615B-411B-86E2-584C6ABC735C}" 21 | ProjectSection(SolutionItems) = preProject 22 | LICENSE = LICENSE 23 | logo.png = logo.png 24 | README.md = README.md 25 | EndProjectSection 26 | EndProject 27 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApp", "samples\SampleApp\SampleApp.csproj", "{08DB2EA7-36E9-4F1F-A7A4-924C164EE0F4}" 28 | EndProject 29 | Global 30 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 31 | Debug|Any CPU = Debug|Any CPU 32 | Release|Any CPU = Release|Any CPU 33 | EndGlobalSection 34 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 35 | {4A42EACD-20CE-4B0E-92C7-379275A43FD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {4A42EACD-20CE-4B0E-92C7-379275A43FD3}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {4A42EACD-20CE-4B0E-92C7-379275A43FD3}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {4A42EACD-20CE-4B0E-92C7-379275A43FD3}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {08DB2EA7-36E9-4F1F-A7A4-924C164EE0F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {08DB2EA7-36E9-4F1F-A7A4-924C164EE0F4}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {08DB2EA7-36E9-4F1F-A7A4-924C164EE0F4}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {08DB2EA7-36E9-4F1F-A7A4-924C164EE0F4}.Release|Any CPU.Build.0 = Release|Any CPU 43 | EndGlobalSection 44 | GlobalSection(SolutionProperties) = preSolution 45 | HideSolutionNode = FALSE 46 | EndGlobalSection 47 | GlobalSection(NestedProjects) = preSolution 48 | {81766A62-D89D-40A4-8F2F-2BC0ADAEE204} = {6FA8B159-AC0A-4680-9D1D-3AB55D63CC83} 49 | {08DB2EA7-36E9-4F1F-A7A4-924C164EE0F4} = {B80E60CD-2BAE-4905-B305-EB5A1EC5199E} 50 | EndGlobalSection 51 | GlobalSection(ExtensibilityGlobals) = postSolution 52 | SolutionGuid = {CD3998F4-DC01-4D6A-8163-5458EE06FF01} 53 | EndGlobalSection 54 | EndGlobal 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![](https://img.shields.io/nuget/v/LinqConditionalExtensions.svg)](https://www.nuget.org/packages/LinqConditionalExtensions) [![](https://img.shields.io/nuget/vpre/LinqConditionalExtensions.svg)](https://www.nuget.org/packages/LinqConditionalExtensions) 2 | 3 | # LinqConditionalExtensions 4 | These extensions make it easy to chain Linq expressions based on conditions—useful for sorting, filtering, and paging. 5 | 6 | ## Installation 7 | ### Package Manager 8 | `Install-Package LinqConditionalExtensions` 9 | 10 | ### .NET CLI 11 | `dotnet add package LinqConditionalExtensions` 12 | 13 | ## Conditionals 14 | Any extension for `IEnumerable` and `IQueryable` that returns itself can be called with `If` appended to it and be conditionally applied. 15 | 16 | Here is a sample for applying where clauses conditionally based on filters, ordering by a column, and paging the results. When using something like Entity Framework, this entire chain will be dynamically converted straight to SQL and make your query time much shorter. 17 | ```csharp 18 | var results = _context.Employees 19 | .WhereIf(hasNameFilter, e => e.Name.Contains(nameFilter)) 20 | .WhereIf(hasPositionFilter, e => e.Position.Contains(positionFilter)) 21 | .WhereIf(hasIdFilter, e => e.Id == idFilter) 22 | .OrderByIf(columnSort == "Name", e => e.Name) 23 | .OrderByIf(columnSort == "Position", e => e.Position) 24 | .OrderByIf(columnSort == "Id", e => e.Id) 25 | .SkipIf(isPaged, pageNumber * resultsPerPage) 26 | .TakeIf(isPaged, resultsPerPage) 27 | .ToList(); 28 | ``` 29 | 30 | ## If 31 | You can do anything based on a condition with a single if statement. In this example, if the condition is true, a where clause and order by clause is being applied to the list of employees. 32 | 33 | ```csharp 34 | var query = employeeDirectory 35 | .If(hasNameFilter, employees => employees 36 | .Where(e => e.Name.Contains(nameFilter)) 37 | .OrderBy(e => e.Department)); 38 | ``` 39 | 40 | ## If Chain 41 | You can use an if-chain to add if statement logic to your queryable or enumerable. If chains require you to have an `Else()` call to end the statement. You can add as many `ElseIf()` conditions in between. 42 | 43 | In this sample, a position and name is being used to filter a list of employees to find only the ones who fall under them on the org chart. The CEO returns all employees. The vice president and manager are used to filter by their respective properties. Otherwise, no employees are returned by applying a where false. 44 | 45 | ```csharp 46 | var subordinates = employeeDirectory 47 | .IfChain(position == "CEO", employees => employees) \\ All employees are under the CEO 48 | .ElseIf(position == "VP", employees => employees 49 | .Where(employee => employee.VicePresidentName == name)) \\ Employees that have a vice president with the passed name 50 | .ElseIf(position == "Manager", employees => employees 51 | .Where(employee => employee.ManagerName == name)) \\ Employees that have a manager with the passed name 52 | .Else(employees => employees 53 | .Where(employee => false)); \\ No employees 54 | ``` 55 | 56 | In this sample, instead of returning the subordinates, we are just getting the count. You can do a transformation on the result. You just have to make sure each method in the chain returns the same type. 57 | 58 | ```csharp 59 | var subordinateCount = employeeDirectory 60 | .IfChain(position == "CEO", employees => employees.Count()) 61 | .ElseIf(position == "VP", employees => employees.Where(employee => employee.VicePresidentName == name).Count()) 62 | .ElseIf(position == "Manager", employees => employees.Where(employee => employee.ManagerName == name).Count()) 63 | .Else(employees => 0); 64 | ``` 65 | 66 | ## Switch 67 | You can use a switch statement to chain together case statements. This is really useful for applying sorts. 68 | 69 | In this example, a switch chain is being used to order results based on the column sort string. 70 | 71 | ```csharp 72 | var sortedResults = results 73 | .Switch(columnSort) 74 | .Case("Name", set => set.OrderBy(e => e.Name)) 75 | .Case("Position", set => set.OrderBy(e => e.Position)) 76 | .Case("VicePresidentName", set => set.OrderBy(e => e.VicePresidentName)) 77 | .Case("ManagerName", set => set.OrderBy(e => e.ManagerName)) 78 | .Default(); 79 | ``` 80 | 81 | Since ordering is a common task, you can use the shorthand version with order by case. There is also a where case for quickly applying filters in a switch. 82 | 83 | ```csharp 84 | var sortedResults = results 85 | .Switch(columnSort) 86 | .OrderByCase("Name", e => e.Name) 87 | .OrderByCase("Position", e => e.Position) 88 | .OrderByCase("VicePresidentName", e => e.VicePresidentName) 89 | .OrderByCase("ManagerName", e => e.ManagerName) 90 | .Default(); 91 | ``` 92 | 93 | Also, there are overloads to use lamba expressions for each case if you need to do something more complex. For example, you can use a class or tuple to include the direction of a sort. 94 | 95 | **Note:** Only the first true condition will be applied. The rest of the conditions will be ignored. If there is overlap, make sure you order your conditions appropriately. 96 | 97 | ```csharp 98 | var sort = (column: "Name", direction: "asc"); 99 | 100 | var sortedResults3 = employeeDirectory 101 | .Switch(sort) 102 | .OrderByCase(e => e.column == "Name" && e.direction == "asc", e => e.Name) 103 | .OrderByDescendingCase(e => e.column == "Name" && e.direction == "desc", e => e.Name) 104 | .OrderByCase(e => e.column == "Position" && e.direction == "asc", e => e.Position) 105 | .OrderByDescendingCase(e => e.column == "Position" && e.direction == "desc", e => e.Position) 106 | .OrderByCase(e => e.column == "VicePresidentName" && e.direction == "asc", e => e.VicePresidentName) 107 | .OrderByDescendingCase(e => e.column == "VicePresidentName" && e.direction == "desc", e => e.VicePresidentName) 108 | .OrderByCase(e => e.column == "ManagerName" && e.direction == "asc", e => e.VicePresidentName) 109 | .OrderByDescendingCase(e => e.column == "ManagerName" && e.direction == "desc", e => e.VicePresidentName) 110 | .OrderByDefault(e => e.Name); 111 | ``` 112 | 113 | You can also do a transformation in the switch chain, but you'll have to specify the type. 114 | 115 | In this example, the types are string for the switch, Employee for the source, and int for the result. 116 | 117 | ```csharp 118 | var total = employeeDirectory 119 | .Switch(department) 120 | .Case("IT", set => set.Where(e => e.Department == "Information Technology").Count()) 121 | .Default(set => 0); 122 | ``` 123 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/todd-skelton/LinqConditionalExtensions/adbc453b84eefcf219e2e55820a4287608aca868/logo.png -------------------------------------------------------------------------------- /samples/SampleApp/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel; 3 | using System.Linq; 4 | 5 | namespace SampleApp 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var employeeDirectory = new List() 12 | { 13 | new Employee{Name= "Raymond Madigan", Department="IT", Position = "VP"}, 14 | new Employee{Name= "Lawanda Damon", Department="IT", Position = "Manager", VicePresidentName="Raymond Madigan"}, 15 | new Employee{Name= "Fred Speier", Department="IT", ManagerName ="", Position = "Engineer", VicePresidentName="Raymond Madigan"}, 16 | new Employee{Name= "Nadine Cook", Department="HR", Position = "VP"}, 17 | new Employee{Name= "David Burnett", Department="HR", Position = "Manager", VicePresidentName="Nadine Cook"}, 18 | new Employee{Name= "John Hernandez", Department="HR", ManagerName ="", Position = "Recruiter", VicePresidentName="Nadine Cook"}, 19 | new Employee{Name= "Richard Miracle", Department="Executive", Position = "CEO"} 20 | }; 21 | 22 | var position = "VP"; 23 | var name = "Raymond Madigan"; 24 | 25 | var subordinates = employeeDirectory 26 | .IfChain(position == "CEO", employees => employees) 27 | .ElseIf(position == "VP", employees => employees.Where(employee => employee.VicePresidentName == name)) 28 | .ElseIf(position == "Manager", employees => employees.Where(employee => employee.ManagerName == name)) 29 | .Else(employees => employees.Where(employee => false)); 30 | 31 | var subordinateCount = employeeDirectory 32 | .IfChain(position == "CEO", employees => employees.Count()) 33 | .ElseIf(position == "VP", employees => employees.Where(employee => employee.VicePresidentName == name).Count()) 34 | .ElseIf(position == "Manager", employees => employees.Where(employee => employee.ManagerName == name).Count()) 35 | .Else(employees => 0); 36 | 37 | var columnSort = "Name"; 38 | 39 | var sortedResults = employeeDirectory 40 | .Switch(columnSort) 41 | .Case("Name", set => set.OrderBy(e => e.Name)) 42 | .Case("Position", set => set.OrderBy(e => e.Position)) 43 | .Case("VicePresidentName", set => set.OrderBy(e => e.VicePresidentName)) 44 | .Case("ManagerName", set => set.OrderBy(e => e.ManagerName)) 45 | .Default(); 46 | 47 | var sortedResults2 = employeeDirectory 48 | .Switch(columnSort) 49 | .OrderByCase("Name", e => e.Name) 50 | .OrderByCase("Position", e => e.Position) 51 | .OrderByCase("VicePresidentName", e => e.VicePresidentName) 52 | .OrderByCase("ManagerName", e => e.ManagerName) 53 | .Default(); 54 | 55 | var sort = (column: "Name", direction: "asc"); 56 | 57 | var sortedResults3 = employeeDirectory 58 | .Switch(sort) 59 | .OrderByCase(e => e.column == "Name" && e.direction == "asc", e => e.Name) 60 | .OrderByDescendingCase(e => e.column == "Name" && e.direction == "desc", e => e.Name) 61 | .OrderByCase(e => e.column == "Position" && e.direction == "asc", e => e.Position) 62 | .OrderByDescendingCase(e => e.column == "Position" && e.direction == "desc", e => e.Position) 63 | .OrderByCase(e => e.column == "VicePresidentName" && e.direction == "asc", e => e.VicePresidentName) 64 | .OrderByDescendingCase(e => e.column == "VicePresidentName" && e.direction == "desc", e => e.VicePresidentName) 65 | .OrderByCase(e => e.column == "ManagerName" && e.direction == "asc", e => e.VicePresidentName) 66 | .OrderByDescendingCase(e => e.column == "ManagerName" && e.direction == "desc", e => e.VicePresidentName) 67 | .OrderByDefault(e => e.Name); 68 | 69 | var department = "IT"; 70 | 71 | int total = employeeDirectory 72 | .Switch(department) 73 | .Case("IT", set => set.Where(e => e.Department == "Information Technology").Count()) 74 | .Case("HR", set => set.Where(e => e.Department == "Human Resources").Count()) 75 | .Default(set => 0); 76 | 77 | var condition = true; 78 | 79 | var query = employeeDirectory 80 | .If(condition, employees => employees 81 | .Where(e => e.Name == "Something") 82 | .OrderBy(e => e.Department)); 83 | } 84 | 85 | public class Employee 86 | { 87 | public string Name { get; set; } 88 | public string Position { get; set; } 89 | public string VicePresidentName { get; set; } 90 | public string ManagerName { get; set; } 91 | public string Department { get; set; } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /samples/SampleApp/SampleApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | netstandard2.0 4 | true 5 | 1.3.0 6 | Todd Skelton 7 | Kloc Limited Company 8 | Linq Conditional Extensions 9 | Linq Conditional Extensions 10 | https://github.com/xKloc/LinqConditionalExtensions/blob/master/logo.png?raw=true 11 | https://github.com/xKloc/LinqConditionalExtensions 12 | MIT 13 | https://github.com/xKloc/LinqConditionalExtensions 14 | git 15 | linq if where when then switch case extensions expressions sort filter page sorting filtering paging 16 | Added order by and where default extensions for switch statements. 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/ConditionalEnumerableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | public static class ConditionalEnumerableExtensions 6 | { 7 | public static IEnumerable If(this IEnumerable source, bool condition, Func, IEnumerable> expression) 8 | => condition ? expression.Invoke(source) : source; 9 | 10 | public static IConditionalEnumerable If(this IConditionalEnumerable conditional, bool condition, Func, IEnumerable> expression) 11 | => condition ? new ConditionalEnumerable(conditional.Source, e => expression(conditional.Expression(e)), condition) : new ConditionalEnumerable(conditional.Source, expression, condition); 12 | 13 | public static IConditionalEnumerable IfChain(this IEnumerable source, bool condition, Func, IEnumerable> expression) 14 | => new ConditionalEnumerable(source, expression, condition); 15 | 16 | public static IConditionalEnumerable IfChain(this IEnumerable source, bool condition, Func, TResult> expression) 17 | => new ConditionalEnumerable(source, expression, condition); 18 | 19 | public static IConditionalEnumerable ElseIf(this IConditionalEnumerable conditional, bool condition, Func, IEnumerable> expression) 20 | => conditional.IsMet ? conditional : new ConditionalEnumerable(conditional.Source, expression, condition); 21 | 22 | public static IConditionalEnumerable ElseIf(this IConditionalEnumerable conditional, bool condition, Func, TResult> expression) 23 | => conditional.IsMet ? conditional : new ConditionalEnumerable(conditional.Source, expression, condition); 24 | 25 | public static IEnumerable Else(this IConditionalEnumerable conditional) 26 | => conditional.IsMet ? conditional.Expression.Invoke(conditional.Source) : conditional.Source; 27 | 28 | public static IEnumerable Else(this IConditionalEnumerable conditional, Func, IEnumerable> expression) 29 | => conditional.IsMet ? conditional.Expression.Invoke(conditional.Source) : expression.Invoke(conditional.Source); 30 | 31 | public static TResult Else(this IConditionalEnumerable conditional, Func, TResult> expression) 32 | => conditional.IsMet ? conditional.Expression.Invoke(conditional.Source) : expression.Invoke(conditional.Source); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/ConditionalQueryableExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | public static class ConditionalQueryableExtensions 4 | { 5 | public static IQueryable If(this IQueryable source, bool condition, Func, IQueryable> expression) 6 | => condition ? expression.Invoke(source) : source; 7 | 8 | public static IConditionalQueryable If(this IConditionalQueryable conditional, bool condition, Func, IQueryable> expression) 9 | => condition ? new ConditionalQueryable(conditional.Source, e => expression(conditional.Expression(e)), condition) : new ConditionalQueryable(conditional.Source, expression, condition); 10 | 11 | public static IConditionalQueryable IfChain(this IQueryable source, bool condition, Func, IQueryable> expression) 12 | => new ConditionalQueryable(source, expression, condition); 13 | 14 | public static IConditionalQueryable IfChain(this IQueryable source, bool condition, Func, TResult> expression) 15 | => new ConditionalQueryable(source, expression, condition); 16 | 17 | public static IConditionalQueryable ElseIf(this IConditionalQueryable conditional, bool condition, Func, IQueryable> expression) 18 | => conditional.IsMet ? conditional : new ConditionalQueryable(conditional.Source, expression, condition); 19 | 20 | public static IConditionalQueryable ElseIf(this IConditionalQueryable conditional, bool condition, Func, TResult> expression) 21 | => conditional.IsMet ? conditional : new ConditionalQueryable(conditional.Source, expression, condition); 22 | 23 | public static IQueryable Else(this IConditionalQueryable conditional) 24 | => conditional.IsMet ? conditional.Expression.Invoke(conditional.Source) : conditional.Source; 25 | 26 | public static IQueryable Else(this IConditionalQueryable conditional, Func, IQueryable> expression) 27 | => conditional.IsMet ? conditional.Expression.Invoke(conditional.Source) : expression.Invoke(conditional.Source); 28 | 29 | public static TResult Else(this IConditionalQueryable conditional, Func, TResult> expression) 30 | => conditional.IsMet ? conditional.Expression.Invoke(conditional.Source) : expression.Invoke(conditional.Source); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/EnumerableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | public static class EnumerableExtensions 6 | { 7 | public static IEnumerable AppendIf(this IEnumerable source, bool condition, TSource element) 8 | => condition ? source.Append(element) : source; 9 | 10 | public static IEnumerable ConcatIf(this IEnumerable first, bool condition, IEnumerable second) 11 | => condition ? first.Concat(second) : first; 12 | 13 | public static IEnumerable DistinctIf(this IEnumerable source, bool condition) 14 | => condition ? source.Distinct() : source; 15 | 16 | public static IEnumerable DistinctIf(this IEnumerable source, bool condition, IEqualityComparer comparer) 17 | => condition ? source.Distinct(comparer) : source; 18 | 19 | public static IEnumerable ExceptIf(this IEnumerable first, bool condition, IEnumerable second) 20 | => condition ? first.Except(second) : first; 21 | 22 | public static IEnumerable ExceptIf(this IEnumerable first, bool condition, IEnumerable second, IEqualityComparer comparer) 23 | => condition ? first.Except(second, comparer) : first; 24 | 25 | public static IEnumerable IntersectIf(this IEnumerable first, bool condition, IEnumerable second) 26 | => condition ? first.Intersect(second) : first; 27 | 28 | public static IEnumerable IntersectIf(this IEnumerable first, bool condition, IEnumerable second, IEqualityComparer comparer) 29 | => condition ? first.Intersect(second, comparer) : first; 30 | 31 | public static IOrderedEnumerable OrderByIf(this IEnumerable source, bool condition, Func keySelector, IComparer comparer) 32 | => condition ? source.OrderBy(keySelector, comparer) : source.OrderBy(e => true); 33 | 34 | public static IOrderedEnumerable OrderByIf(this IEnumerable source, bool condition, Func keySelector) 35 | => condition ? source.OrderBy(keySelector) : source.OrderBy(e => true); 36 | 37 | public static IOrderedEnumerable OrderByDescendingIf(this IEnumerable source, bool condition, Func keySelector) 38 | => condition ? source.OrderByDescending(keySelector) : source.OrderByDescending(e => true); 39 | 40 | public static IOrderedEnumerable OrderByDescendingIf(this IEnumerable source, bool condition, Func keySelector, IComparer comparer) 41 | => condition ? source.OrderByDescending(keySelector, comparer) : source.OrderByDescending(e => true); 42 | 43 | public static IEnumerable PrependIf(this IEnumerable source, bool condition, TSource element) 44 | => condition ? source.Prepend(element) : source; 45 | 46 | public static IEnumerable ReverseIf(this IEnumerable source, bool condition) 47 | => condition ? source.Reverse() : source; 48 | 49 | public static IEnumerable SkipIf(this IEnumerable source, bool condition, int count) 50 | => condition ? source.Skip(count) : source; 51 | 52 | public static IEnumerable SkipWhileIf(this IEnumerable source, bool condition, Func predicate) 53 | => condition ? source.SkipWhile(predicate) : source; 54 | 55 | public static IEnumerable SkipWhileIf(this IEnumerable source, bool condition, Func predicate) 56 | => condition ? source.SkipWhile(predicate) : source; 57 | 58 | public static IEnumerable TakeIf(this IEnumerable source, bool condition, int count) 59 | => condition ? source.Take(count) : source; 60 | 61 | public static IEnumerable TakeWhileIf(this IEnumerable source, bool condition, Func predicate) 62 | => condition ? source.TakeWhile(predicate) : source; 63 | 64 | public static IEnumerable TakeWhileIf(this IEnumerable source, bool condition, Func predicate) 65 | => condition ? source.TakeWhile(predicate) : source; 66 | 67 | public static IOrderedEnumerable ThenByIf(this IOrderedEnumerable source, bool condition, Func keySelector) 68 | => condition ? source.ThenBy(keySelector) : source; 69 | 70 | public static IOrderedEnumerable ThenByIf(this IOrderedEnumerable source, bool condition, Func keySelector, IComparer comparer) 71 | => condition ? source.ThenBy(keySelector, comparer) : source; 72 | 73 | public static IOrderedEnumerable ThenByDescendingIf(this IOrderedEnumerable source, bool condition, Func keySelector, IComparer comparer) 74 | => condition ? source.ThenByDescending(keySelector, comparer) : source; 75 | 76 | public static IOrderedEnumerable ThenByDescendingIf(this IOrderedEnumerable source, bool condition, Func keySelector) 77 | => condition ? source.ThenByDescending(keySelector) : source; 78 | 79 | public static IEnumerable UnionIf(this IEnumerable first, bool condition, IEnumerable second) 80 | => condition ? first.Union(second) : first; 81 | 82 | public static IEnumerable UnionIf(this IEnumerable first, bool condition, IEnumerable second, IEqualityComparer comparer) 83 | => condition ? first.Union(second, comparer) : first; 84 | 85 | public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) 86 | => condition ? source.Where(predicate) : source; 87 | 88 | public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) 89 | => condition ? source.Where(predicate) : source; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/IConditionalEnumerable`1.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | public interface IConditionalEnumerable : IConditionalEnumerable> 6 | { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/IConditionalEnumerable`2.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | public interface IConditionalEnumerable 6 | { 7 | IEnumerable Source { get; } 8 | Func, TResult> Expression { get; } 9 | bool IsMet { get; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/IConditionalQueryable`1.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | public interface IConditionalQueryable : IConditionalQueryable> 4 | { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/IConditionalQueryable`2.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | public interface IConditionalQueryable 4 | { 5 | IQueryable Source { get; } 6 | Func, TResult> Expression { get; } 7 | bool IsMet { get; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/ISwitchableEnumerable`2.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | public interface ISwitchableEnumerable : ISwitchableEnumerable> 6 | { 7 | TSwitch Switch { get; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/ISwitchableEnumerable`3.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | public interface ISwitchableEnumerable : IConditionalEnumerable 4 | { 5 | TSwitch Switch { get; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/ISwitchableQueryable`2.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | public interface ISwitchableQueryable : ISwitchableQueryable> 4 | { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/ISwitchableQueryable`3.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | public interface ISwitchableQueryable : IConditionalQueryable 4 | { 5 | TSwitch Switch { get; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/ConditionalEnumerable`1.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | internal class ConditionalEnumerable : IConditionalEnumerable 6 | { 7 | public ConditionalEnumerable(IEnumerable source, Func, IEnumerable> expression, bool isMet) 8 | { 9 | Source = source ?? throw new ArgumentNullException(nameof(source)); 10 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 11 | IsMet = isMet; 12 | } 13 | 14 | public IEnumerable Source { get; } 15 | 16 | public Func, IEnumerable> Expression { get; } 17 | 18 | public bool IsMet { get; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/ConditionalEnumerable`2.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | internal class ConditionalEnumerable : IConditionalEnumerable 6 | { 7 | public ConditionalEnumerable(IEnumerable source, Func, TResult> expression, bool isMet) 8 | { 9 | Source = source ?? throw new ArgumentNullException(nameof(source)); 10 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 11 | IsMet = isMet; 12 | } 13 | 14 | public IEnumerable Source { get; } 15 | 16 | public Func, TResult> Expression { get; } 17 | 18 | public bool IsMet { get; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/ConditionalQueryable`1.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | internal class ConditionalQueryable : IConditionalQueryable 4 | { 5 | public ConditionalQueryable(IQueryable source, Func, IQueryable> expression, bool isMet) 6 | { 7 | Source = source ?? throw new ArgumentNullException(nameof(source)); 8 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 9 | IsMet = isMet; 10 | } 11 | 12 | public IQueryable Source { get; } 13 | 14 | public Func, IQueryable> Expression { get; } 15 | 16 | public bool IsMet { get; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/ConditionalQueryable`2.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | internal class ConditionalQueryable : IConditionalQueryable 4 | { 5 | public ConditionalQueryable(IQueryable source, Func, TResult> expression, bool isMet) 6 | { 7 | Source = source ?? throw new ArgumentNullException(nameof(source)); 8 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 9 | IsMet = isMet; 10 | } 11 | 12 | public IQueryable Source { get; } 13 | 14 | public Func, TResult> Expression { get; } 15 | 16 | public bool IsMet { get; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/SwitchableEnumerable`2.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | internal class SwitchableEnumerable : ISwitchableEnumerable 6 | { 7 | public SwitchableEnumerable(IEnumerable source, TSwitch @switch) 8 | { 9 | Source = source ?? throw new ArgumentNullException(nameof(source)); 10 | Switch = @switch; 11 | Expression = null; 12 | IsMet = false; 13 | } 14 | 15 | public SwitchableEnumerable(IEnumerable source, TSwitch @switch, Func, IEnumerable> expression, bool isMet) 16 | { 17 | Source = source ?? throw new ArgumentNullException(nameof(source)); 18 | Switch = @switch; 19 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 20 | IsMet = isMet; 21 | } 22 | 23 | public IEnumerable Source { get; } 24 | 25 | public TSwitch Switch { get; } 26 | 27 | public Func, IEnumerable> Expression { get; } 28 | 29 | public bool IsMet { get; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/SwitchableEnumerable`3.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | internal class SwitchableEnumerable : ISwitchableEnumerable 6 | { 7 | public SwitchableEnumerable(IEnumerable source, TSwitch @switch) 8 | { 9 | Source = source ?? throw new ArgumentNullException(nameof(source)); 10 | Switch = @switch; 11 | Expression = null; 12 | IsMet = false; 13 | } 14 | 15 | public SwitchableEnumerable(IEnumerable source, TSwitch @switch, Func, TResult> expression, bool isMet) 16 | { 17 | Source = source ?? throw new ArgumentNullException(nameof(source)); 18 | Switch = @switch; 19 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 20 | IsMet = isMet; 21 | } 22 | 23 | public IEnumerable Source { get; } 24 | 25 | public TSwitch Switch { get; } 26 | 27 | public Func, TResult> Expression { get; } 28 | 29 | public bool IsMet { get; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/SwitchableQueryable`2.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | internal class SwitchableQueryable : ISwitchableQueryable 4 | { 5 | public SwitchableQueryable(IQueryable source, TSwitch @switch) 6 | { 7 | Source = source ?? throw new ArgumentNullException(nameof(source)); 8 | Switch = @switch; 9 | Expression = null; 10 | IsMet = false; 11 | } 12 | 13 | public SwitchableQueryable(IQueryable source, TSwitch @switch, Func, IQueryable> expression, bool isMet) 14 | { 15 | Source = source ?? throw new ArgumentNullException(nameof(source)); 16 | Switch = @switch; 17 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 18 | IsMet = isMet; 19 | } 20 | 21 | public IQueryable Source { get; } 22 | 23 | public TSwitch Switch { get; } 24 | 25 | public Func, IQueryable> Expression { get; } 26 | 27 | public bool IsMet { get; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/Internal/SwitchableQueryable`3.cs: -------------------------------------------------------------------------------- 1 | namespace System.Linq 2 | { 3 | internal class SwitchableQueryable : ISwitchableQueryable 4 | { 5 | public SwitchableQueryable(IQueryable source, TSwitch @switch) 6 | { 7 | Source = source ?? throw new ArgumentNullException(nameof(source)); 8 | Switch = @switch; 9 | Expression = null; 10 | IsMet = false; 11 | } 12 | 13 | public SwitchableQueryable(IQueryable source, TSwitch @switch, Func, TResult> expression, bool isMet) 14 | { 15 | Source = source ?? throw new ArgumentNullException(nameof(source)); 16 | Switch = @switch; 17 | Expression = expression ?? throw new ArgumentNullException(nameof(expression)); 18 | IsMet = isMet; 19 | } 20 | 21 | public IQueryable Source { get; } 22 | 23 | public TSwitch Switch { get; } 24 | 25 | public Func, TResult> Expression { get; } 26 | 27 | public bool IsMet { get; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/LinqConditionalExtensions.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netstandard2.0 4 | System.Linq 5 | C:\Users\admin\Source\Repos\LinqConditionalExtensions\src\LinqConditionalExtensions\LinqConditionalExtensions.xml 6 | These extensions make it easy to chain Linq expressions based on conditions—useful for sorting, filtering, and paging. 7 | 8 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/LinqConditionalExtensions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LinqConditionalExtensions 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/QueryableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | 4 | namespace System.Linq 5 | { 6 | public static class QueryableExtensions 7 | { 8 | public static IQueryable ConcatIf(this IQueryable first, bool condition, IQueryable second) 9 | => condition ? first.Concat(second) : first; 10 | 11 | public static IQueryable DistinctIf(this IQueryable source, bool condition) 12 | => condition ? source.Distinct() : source; 13 | 14 | public static IQueryable DistinctIf(this IQueryable source, bool condition, IEqualityComparer comparer) 15 | => condition ? source.Distinct(comparer) : source; 16 | 17 | public static IQueryable ExceptIf(this IQueryable first, bool condition, IQueryable second) 18 | => condition ? first.Except(second) : first; 19 | 20 | public static IQueryable ExceptIf(this IQueryable first, bool condition, IQueryable second, IEqualityComparer comparer) 21 | => condition ? first.Except(second, comparer) : first; 22 | 23 | public static IQueryable IntersectIf(this IQueryable first, bool condition, IQueryable second) 24 | => condition ? first.Intersect(second) : first; 25 | 26 | public static IQueryable IntersectIf(this IQueryable first, bool condition, IQueryable second, IEqualityComparer comparer) 27 | => condition ? first.Intersect(second, comparer) : first; 28 | 29 | public static IOrderedQueryable OrderByIf(this IQueryable source, bool condition, Expression> keySelector, IComparer comparer) 30 | => condition ? source.OrderBy(keySelector, comparer) : source.OrderBy(e => true); 31 | 32 | public static IOrderedQueryable OrderByIf(this IQueryable source, bool condition, Expression> keySelector) 33 | => condition ? source.OrderBy(keySelector) : source.OrderBy(e => true); 34 | 35 | public static IOrderedQueryable OrderByDescendingIf(this IQueryable source, bool condition, Expression> keySelector) 36 | => condition ? source.OrderByDescending(keySelector) : source.OrderByDescending(e => true); 37 | 38 | public static IOrderedQueryable OrderByDescendingIf(this IQueryable source, bool condition, Expression> keySelector, IComparer comparer) 39 | => condition ? source.OrderByDescending(keySelector, comparer) : source.OrderByDescending(e => true); 40 | 41 | public static IQueryable ReverseIf(this IQueryable source, bool condition) 42 | => condition ? source.Reverse() : source; 43 | 44 | public static IQueryable SkipIf(this IQueryable source, bool condition, int count) 45 | => condition ? source.Skip(count) : source; 46 | 47 | public static IQueryable SkipWhileIf(this IQueryable source, bool condition, Expression> predicate) 48 | => condition ? source.SkipWhile(predicate) : source; 49 | 50 | public static IQueryable SkipWhileIf(this IQueryable source, bool condition, Expression> predicate) 51 | => condition ? source.SkipWhile(predicate) : source; 52 | 53 | public static IQueryable TakeIf(this IQueryable source, bool condition, int count) 54 | => condition ? source.Take(count) : source; 55 | 56 | public static IQueryable TakeWhileIf(this IQueryable source, bool condition, Expression> predicate) 57 | => condition ? source.TakeWhile(predicate) : source; 58 | 59 | public static IQueryable TakeWhileIf(this IQueryable source, bool condition, Expression> predicate) 60 | => condition ? source.TakeWhile(predicate) : source; 61 | 62 | public static IOrderedQueryable ThenByIf(this IOrderedQueryable source, bool condition, Expression> keySelector) 63 | => condition ? source.ThenBy(keySelector) : source; 64 | 65 | public static IOrderedQueryable ThenByIf(this IOrderedQueryable source, bool condition, Expression> keySelector, IComparer comparer) 66 | => condition ? source.ThenBy(keySelector, comparer) : source; 67 | 68 | public static IOrderedQueryable ThenByDescendingIf(this IOrderedQueryable source, bool condition, Expression> keySelector, IComparer comparer) 69 | => condition ? source.ThenByDescending(keySelector, comparer) : source; 70 | 71 | public static IOrderedQueryable ThenByDescendingIf(this IOrderedQueryable source, bool condition, Expression> keySelector) 72 | => condition ? source.ThenByDescending(keySelector) : source; 73 | 74 | public static IQueryable UnionIf(this IQueryable first, bool condition, IQueryable second) 75 | => condition ? first.Union(second) : first; 76 | 77 | public static IQueryable UnionIf(this IQueryable first, bool condition, IQueryable second, IEqualityComparer comparer) 78 | => condition ? first.Union(second, comparer) : first; 79 | 80 | public static IQueryable WhereIf(this IQueryable source, bool condition, Expression> predicate) 81 | => condition ? source.Where(predicate) : source; 82 | 83 | public static IQueryable WhereIf(this IQueryable source, bool condition, Expression> predicate) 84 | => condition ? source.Where(predicate) : source; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/SwitchableEnumerableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace System.Linq 4 | { 5 | public static class SwitchableEnumerableExtensions 6 | { 7 | public static ISwitchableEnumerable Switch(this IEnumerable source, TSwitch @switch) 8 | => new SwitchableEnumerable(source, @switch); 9 | 10 | public static ISwitchableEnumerable Switch(this IEnumerable source, TSwitch @switch) 11 | => new SwitchableEnumerable(source, @switch); 12 | 13 | public static ISwitchableEnumerable Case(this ISwitchableEnumerable switchable, TSwitch match, Func, IEnumerable> expression) 14 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, expression, match.Equals(switchable.Switch)); 15 | 16 | public static ISwitchableEnumerable Case(this ISwitchableEnumerable switchable, TSwitch match, Func, TResult> expression) 17 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, expression, match.Equals(switchable.Switch)); 18 | 19 | public static ISwitchableEnumerable Case(this ISwitchableEnumerable switchable, Func casePredicate, Func, IEnumerable> expression) 20 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, expression, casePredicate(switchable.Switch)); 21 | 22 | public static ISwitchableEnumerable Case(this ISwitchableEnumerable switchable, Func casePredicate, Func, TResult> expression) 23 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, expression, casePredicate(switchable.Switch)); 24 | 25 | public static IEnumerable Default(this ISwitchableEnumerable switchable) 26 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source; 27 | 28 | public static IEnumerable Default(this ISwitchableEnumerable switchable, Func, IEnumerable> expression) 29 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : expression.Invoke(switchable.Source); 30 | 31 | public static TResult Default(this ISwitchableEnumerable switchable, Func, TResult> expression) 32 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : expression.Invoke(switchable.Source); 33 | 34 | public static ISwitchableEnumerable OrderByCase(this ISwitchableEnumerable switchable, TSwitch match, Func keySelector, IComparer comparer) 35 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector, comparer), match.Equals(switchable.Switch)); 36 | 37 | public static ISwitchableEnumerable OrderByCase(this ISwitchableEnumerable switchable, TSwitch match, Func keySelector) 38 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector), match.Equals(switchable.Switch)); 39 | 40 | public static ISwitchableEnumerable OrderByCase(this ISwitchableEnumerable switchable, Func casePredicate, Func keySelector, IComparer comparer) 41 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector, comparer), casePredicate(switchable.Switch)); 42 | 43 | public static ISwitchableEnumerable OrderByCase(this ISwitchableEnumerable switchable, Func casePredicate, Func keySelector) 44 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector), casePredicate(switchable.Switch)); 45 | 46 | public static ISwitchableEnumerable OrderByDescendingCase(this ISwitchableEnumerable switchable, TSwitch match, Func keySelector, IComparer comparer) 47 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector, comparer), match.Equals(switchable.Switch)); 48 | 49 | public static ISwitchableEnumerable OrderByDescendingCase(this ISwitchableEnumerable switchable, TSwitch match, Func keySelector) 50 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector), match.Equals(switchable.Switch)); 51 | 52 | public static ISwitchableEnumerable OrderByDescendingCase(this ISwitchableEnumerable switchable, Func casePredicate, Func keySelector, IComparer comparer) 53 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector, comparer), casePredicate(switchable.Switch)); 54 | 55 | public static ISwitchableEnumerable OrderByDescendingCase(this ISwitchableEnumerable switchable, Func casePredicate, Func keySelector) 56 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector), casePredicate(switchable.Switch)); 57 | 58 | public static IEnumerable OrderByDefault(this ISwitchableEnumerable switchable, Func keySelector, IComparer comparer) 59 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderBy(keySelector, comparer); 60 | 61 | public static IEnumerable OrderByDefault(this ISwitchableEnumerable switchable, Func keySelector) 62 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderBy(keySelector); 63 | 64 | public static IEnumerable OrderByDescendingDefault(this ISwitchableEnumerable switchable, Func keySelector, IComparer comparer) 65 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderByDescending(keySelector, comparer); 66 | 67 | public static IEnumerable OrderByDescendingDefault(this ISwitchableEnumerable switchable, Func keySelector) 68 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderByDescending(keySelector); 69 | 70 | public static ISwitchableEnumerable WhereCase(this ISwitchableEnumerable switchable, TSwitch match, Func predicate) 71 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.Where(predicate), match.Equals(switchable.Switch)); 72 | 73 | public static ISwitchableEnumerable WhereCase(this ISwitchableEnumerable switchable, TSwitch match, Func predicate) 74 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.Where(predicate), match.Equals(switchable.Switch)); 75 | 76 | public static ISwitchableEnumerable WhereCase(this ISwitchableEnumerable switchable, Func casePredicate, Func predicate) 77 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.Where(predicate), casePredicate(switchable.Switch)); 78 | 79 | public static ISwitchableEnumerable WhereCase(this ISwitchableEnumerable switchable, Func casePredicate, Func predicate) 80 | => switchable.IsMet ? switchable : new SwitchableEnumerable(switchable.Source, switchable.Switch, e => e.Where(predicate), casePredicate(switchable.Switch)); 81 | 82 | public static IEnumerable WhereDefault(this ISwitchableEnumerable switchable, Func predicate) 83 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.Where(predicate); 84 | 85 | public static IEnumerable WhereDefault(this ISwitchableEnumerable switchable, Func predicate) 86 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.Where(predicate); 87 | } 88 | } -------------------------------------------------------------------------------- /src/LinqConditionalExtensions/SwitchableQueryableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | 4 | namespace System.Linq 5 | { 6 | public static class SwitchableQueryableExtensions 7 | { 8 | public static ISwitchableQueryable Switch(this IQueryable source, TSwitch @switch) 9 | => new SwitchableQueryable(source, @switch); 10 | 11 | public static ISwitchableQueryable Switch(this IQueryable source, TSwitch @switch) 12 | => new SwitchableQueryable(source, @switch); 13 | 14 | public static ISwitchableQueryable Case(this ISwitchableQueryable switchable, TSwitch match, Func, IQueryable> expression) 15 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, expression, match.Equals(switchable.Switch)); 16 | 17 | public static ISwitchableQueryable Case(this ISwitchableQueryable switchable, TSwitch match, Func, TResult> expression) 18 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, expression, match.Equals(switchable.Switch)); 19 | 20 | public static ISwitchableQueryable Case(this ISwitchableQueryable switchable, Func casePredicate, Func, IQueryable> expression) 21 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, expression, casePredicate(switchable.Switch)); 22 | 23 | public static ISwitchableQueryable Case(this ISwitchableQueryable switchable, Func casePredicate, Func, TResult> expression) 24 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, expression, casePredicate(switchable.Switch)); 25 | 26 | public static IQueryable Default(this ISwitchableQueryable switchable) 27 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source; 28 | 29 | public static IQueryable Default(this ISwitchableQueryable switchable, Func, IQueryable> expression) 30 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : expression.Invoke(switchable.Source); 31 | 32 | public static TResult Default(this ISwitchableQueryable switchable, Func, TResult> expression) 33 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : expression.Invoke(switchable.Source); 34 | 35 | public static ISwitchableQueryable OrderByCase(this ISwitchableQueryable switchable, TSwitch match, Expression> keySelector, IComparer comparer) 36 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector, comparer), match.Equals(switchable.Switch)); 37 | 38 | public static ISwitchableQueryable OrderByCase(this ISwitchableQueryable switchable, TSwitch match, Expression> keySelector) 39 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector), match.Equals(switchable.Switch)); 40 | 41 | public static ISwitchableQueryable OrderByCase(this ISwitchableQueryable switchable, Func casePredicate, Expression> keySelector, IComparer comparer) 42 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector, comparer), casePredicate(switchable.Switch)); 43 | 44 | public static ISwitchableQueryable OrderByCase(this ISwitchableQueryable switchable, Func casePredicate, Expression> keySelector) 45 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderBy(keySelector), casePredicate(switchable.Switch)); 46 | 47 | public static ISwitchableQueryable OrderByDescendingCase(this ISwitchableQueryable switchable, TSwitch match, Expression> keySelector, IComparer comparer) 48 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector, comparer), match.Equals(switchable.Switch)); 49 | 50 | public static ISwitchableQueryable OrderByDescendingCase(this ISwitchableQueryable switchable, TSwitch match, Expression> keySelector) 51 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector), match.Equals(switchable.Switch)); 52 | 53 | public static ISwitchableQueryable OrderByDescendingCase(this ISwitchableQueryable switchable, Func casePredicate, Expression> keySelector, IComparer comparer) 54 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector, comparer), casePredicate(switchable.Switch)); 55 | 56 | public static ISwitchableQueryable OrderByDescendingCase(this ISwitchableQueryable switchable, Func casePredicate, Expression> keySelector) 57 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.OrderByDescending(keySelector), casePredicate(switchable.Switch)); 58 | 59 | public static IQueryable OrderByDefault(this ISwitchableQueryable switchable, Expression> keySelector, IComparer comparer) 60 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderBy(keySelector, comparer); 61 | 62 | public static IQueryable OrderByDefault(this ISwitchableQueryable switchable, Expression> keySelector) 63 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderBy(keySelector); 64 | 65 | public static IQueryable OrderByDescendingDefault(this ISwitchableQueryable switchable, Expression> keySelector, IComparer comparer) 66 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderByDescending(keySelector, comparer); 67 | 68 | public static IQueryable OrderByDescendingDefault(this ISwitchableQueryable switchable, Expression> keySelector) 69 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.OrderByDescending(keySelector); 70 | 71 | public static ISwitchableQueryable WhereCase(this ISwitchableQueryable switchable, TSwitch match, Expression> predicate) 72 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.Where(predicate), match.Equals(switchable.Switch)); 73 | 74 | public static ISwitchableQueryable WhereCase(this ISwitchableQueryable switchable, TSwitch match, Expression> predicate) 75 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.Where(predicate), match.Equals(switchable.Switch)); 76 | 77 | public static ISwitchableQueryable WhereCase(this ISwitchableQueryable switchable, Func casePredicate, Expression> predicate) 78 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.Where(predicate), casePredicate(switchable.Switch)); 79 | 80 | public static ISwitchableQueryable WhereCase(this ISwitchableQueryable switchable, Func casePredicate, Expression> predicate) 81 | => switchable.IsMet ? switchable : new SwitchableQueryable(switchable.Source, switchable.Switch, e => e.Where(predicate), casePredicate(switchable.Switch)); 82 | 83 | public static IQueryable WhereDefault(this ISwitchableQueryable switchable, Expression> predicate) 84 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.Where(predicate); 85 | 86 | public static IQueryable WhereDefault(this ISwitchableQueryable switchable, Expression> predicate) 87 | => switchable.IsMet ? switchable.Expression.Invoke(switchable.Source) : switchable.Source.Where(predicate); 88 | } 89 | } --------------------------------------------------------------------------------