├── .gitignore ├── CursoLINQ.sln ├── CursoLINQ ├── CursoLINQ.csproj ├── Empresa.cs ├── Modulo 10 │ ├── DistinctDemo.cs │ ├── ExceptDemo.cs │ ├── IntersectDemo.cs │ └── UnionDemo.cs ├── Modulo 11 │ ├── ChunkDemo.cs │ ├── ConcatDemo.cs │ ├── SequenceEqualDemo.cs │ ├── TryGetNonEnumeratedCountDemo.cs │ └── ZipDemo.cs ├── Modulo 2 │ ├── ElementAtDemo.cs │ ├── FirstDemo.cs │ ├── LastDemo.cs │ ├── OfTypeDemo.cs │ ├── SingleDemo.cs │ ├── WhereDemo.cs │ └── WhereObjeto.cs ├── Modulo 3 │ ├── OrderByDemo.cs │ ├── ReverseDemo.cs │ └── ThenByDemo.cs ├── Modulo 4 │ ├── SelectDemo.cs │ └── SelectManyDemo.cs ├── Modulo 5 │ ├── Agregado.cs │ ├── Conteo.cs │ ├── MaxByMinBy.cs │ ├── Promedio.cs │ └── SumMaxMin.cs ├── Modulo 6 │ ├── AllDemo.cs │ ├── AnyDemo.cs │ └── ContainsDemo.cs ├── Modulo 7 │ ├── Paginacion.cs │ ├── SkipYSkipLast.cs │ ├── TakeWhileSkipWhile.cs │ └── TakeYTakeLast.cs ├── Modulo 8 │ ├── GroupByDemo.cs │ └── GroupByEdad.cs ├── Modulo 9 │ ├── GroupJoinDemo.cs │ └── JoinDemo.cs ├── Persona.cs ├── PersonaDTO.cs └── Program.cs └── EjemploEFCore ├── EjemploEFCore.sln └── EjemploEFCore ├── ApplicationDbContext.cs ├── EjemploEFCore.csproj ├── Migrations ├── 20210817150212_Initial.Designer.cs ├── 20210817150212_Initial.cs └── ApplicationDbContextModelSnapshot.cs ├── Persona.cs ├── Program.cs ├── Properties └── launchSettings.json └── cursoLINQ.db /.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 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /CursoLINQ.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31606.5 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CursoLINQ", "CursoLINQ\CursoLINQ.csproj", "{85D1D79B-6439-49AD-80CC-F4D68F856132}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {85D1D79B-6439-49AD-80CC-F4D68F856132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {85D1D79B-6439-49AD-80CC-F4D68F856132}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {85D1D79B-6439-49AD-80CC-F4D68F856132}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {85D1D79B-6439-49AD-80CC-F4D68F856132}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {9417B394-79B0-4744-A5E0-28E2C0DBF0A7} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /CursoLINQ/CursoLINQ.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CursoLINQ/Empresa.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ 8 | { 9 | public class Empresa 10 | { 11 | public int Id { get; set; } 12 | public string Nombre { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 10/DistinctDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_10 8 | { 9 | public class DistinctDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 15 | new Persona { Nombre = "Nidia", EmpresaId = 1 }, 16 | new Persona { Nombre = "Eduardo"}, 17 | new Persona { Nombre = "Esmerlin", EmpresaId = 3 } 18 | }; 19 | 20 | int[] numeros = { 1, 2, 3, 1, 1, 6 }; 21 | 22 | var numerosSinRepeticiones = numeros.Distinct(); 23 | 24 | var personasSinNombresRepetidos = personas.DistinctBy(x => x.Nombre); 25 | 26 | // Sintaxis de queries 27 | var numerosSinRepeticiones_2 = from n in numeros.Distinct() 28 | select n; 29 | 30 | var personasSinNombresRepetidos_2 = from p in personas.DistinctBy(x => x.Nombre) 31 | select p; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 10/ExceptDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_10 8 | { 9 | public class ExceptDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personasA = new List() { 14 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 15 | new Persona { Nombre = "Nidia", EmpresaId = 1 }, 16 | new Persona { Nombre = "Esmerlin", EmpresaId = 3 } 17 | }; 18 | 19 | var personasB = new List() 20 | { 21 | new Persona{Nombre = "Fernando", Edad = 25}, 22 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 23 | }; 24 | 25 | int[] A = { 1, 2, 3 }; 26 | 27 | int[] B = { 1, 15 }; 28 | 29 | var NumerosEnAQueNoEstanEnB = A.Except(B); 30 | 31 | var personasBNombres = personasB.Select(x => x.Nombre); 32 | var personasEnAQueNoEstanEnB = personasA.ExceptBy(personasBNombres, x => x.Nombre); 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 10/IntersectDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_10 8 | { 9 | public class IntersectDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personasA = new List() { 14 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 15 | new Persona { Nombre = "Nidia", EmpresaId = 1 }, 16 | new Persona { Nombre = "Esmerlin", EmpresaId = 3 } 17 | }; 18 | 19 | var personasB = new List() 20 | { 21 | new Persona{Nombre = "Fernando", Edad = 25}, 22 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 23 | }; 24 | 25 | int[] A = { 1, 2, 3 }; 26 | 27 | int[] B = { 1, 15 }; 28 | 29 | var numerosQueEstanEnAyB = A.Intersect(B); 30 | 31 | var personasBNombres = personasB.Select(x => x.Nombre); 32 | var personasQueEstanEnAyB = personasA.IntersectBy(personasBNombres, x => x.Nombre); 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 10/UnionDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_10 8 | { 9 | public class UnionDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personasA = new List() { 14 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 15 | new Persona { Nombre = "Nidia", EmpresaId = 1 }, 16 | new Persona { Nombre = "Esmerlin", EmpresaId = 3 } 17 | }; 18 | 19 | var personasB = new List() 20 | { 21 | new Persona{Nombre = "Fernando", Edad = 25}, 22 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 23 | }; 24 | 25 | int[] numerosA = { 1, 2, 3, 1, 1, 6 }; 26 | 27 | int[] numerosB = { 1, 2, 15 }; 28 | 29 | var unionDeNumeros = numerosA.Union(numerosB); 30 | 31 | var unionDePersonas = personasA.UnionBy(personasB, x => x.Nombre); 32 | 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 11/ChunkDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_11 8 | { 9 | public class ChunkDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 14 | 15 | var resultado = A.Chunk(3); 16 | 17 | // Nota: no existe sintaxis de query para este código. 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 11/ConcatDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_11 8 | { 9 | public class ConcatDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | int[] A = { 1, 2, 3 }; 14 | 15 | int[] B = { 4, 5, 6 }; 16 | 17 | var resultado = A.Concat(B); 18 | 19 | // Nota: no existe sintaxis de query para este código. 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 11/SequenceEqualDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_11 8 | { 9 | public class SequenceEqualDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | string[] A = { "felipe", "fernando" }; 14 | 15 | string[] B = { "felipe", "fernando" }; 16 | 17 | string[] C = { "FEliPE", "FernandO" }; 18 | 19 | string[] D = { "fernando", "felipe" }; 20 | 21 | var AB = A.SequenceEqual(B); 22 | 23 | var AC = A.SequenceEqual(C); 24 | 25 | var ACSinImportarMayusculas = A.SequenceEqual(C, StringComparer.OrdinalIgnoreCase); 26 | 27 | var AD = A.SequenceEqual(D); 28 | 29 | // Nota: no existe sintaxis de query para este código. 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 11/TryGetNonEnumeratedCountDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_11 8 | { 9 | public class TryGetNonEnumeratedCountDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 14 | 15 | var conteo = 0; 16 | var pudoContarASinEnumerar = A.TryGetNonEnumeratedCount(out conteo); 17 | 18 | var personasA = new List() 19 | { 20 | new Persona{Nombre = "Eduardo", EmpresaId = 1}, 21 | new Persona{Nombre = "Nidia", EmpresaId = 1}, 22 | new Persona{Nombre = "Esmerlin", EmpresaId = 3} 23 | }; 24 | 25 | var personasCollection = new PersonasCollection(); 26 | personasCollection.AddRange(personasA); 27 | 28 | var conteoPersonas2 = 0; 29 | var pudoContarPersonasSinEnumerar = personasCollection.TryGetNonEnumeratedCount(out conteoPersonas2); 30 | } 31 | 32 | public class PersonasCollection : IEnumerable, ICollection 33 | { 34 | private readonly List _personas = new List(); 35 | 36 | public int Count => _personas.Count; 37 | 38 | public bool IsReadOnly => throw new NotImplementedException(); 39 | 40 | public void Add(Persona persona) 41 | { 42 | _personas.Add(persona); 43 | } 44 | 45 | public void AddRange(IEnumerable personas) 46 | { 47 | _personas.AddRange(personas); 48 | } 49 | 50 | public void Clear() 51 | { 52 | throw new NotImplementedException(); 53 | } 54 | 55 | public bool Contains(Persona item) 56 | { 57 | throw new NotImplementedException(); 58 | } 59 | 60 | public void CopyTo(Persona[] array, int arrayIndex) 61 | { 62 | throw new NotImplementedException(); 63 | } 64 | 65 | public IEnumerator GetEnumerator() 66 | { 67 | return _personas.GetEnumerator(); 68 | } 69 | 70 | public bool Remove(Persona item) 71 | { 72 | throw new NotImplementedException(); 73 | } 74 | 75 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 76 | { 77 | return ((IEnumerable)_personas).GetEnumerator(); 78 | } 79 | } 80 | 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 11/ZipDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_11 8 | { 9 | public class ZipDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | int[] A = { 1, 2, 3 }; 14 | int[] B = { 4, 5, 6 }; 15 | 16 | var combinados = A.Zip(B); 17 | foreach (var combinacion in combinados) 18 | { 19 | Console.WriteLine($"({combinacion.First}, {combinacion.Second})"); 20 | } 21 | 22 | var resultado = A.Zip(B, (a, b) => a * b); 23 | 24 | // Nota: no existe sintaxis de query para este código. 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/ElementAtDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class ElementAtDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 45, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 24, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | var terceraPersona = personas.ElementAt(2); 22 | var sextaPersona = personas.ElementAtOrDefault(5); 23 | 24 | // Sintaxis de queries 25 | var sextaPersona_2 = (from p in personas 26 | select p).ElementAtOrDefault(5); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/FirstDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class FirstDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 45, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 24, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | var primeraPersona = personas.First(); 22 | var primeraPersona2 = personas.FirstOrDefault(); 23 | 24 | var paises = new List(); 25 | try 26 | { 27 | var primerPais = paises.First(); 28 | } 29 | catch (Exception ex) 30 | { 31 | Console.WriteLine("ha ocurrido un error"); 32 | } 33 | 34 | var primerPais2 = paises.FirstOrDefault(); 35 | 36 | var numeros = new List(); 37 | var primerNumero = numeros.FirstOrDefault(); 38 | 39 | var primeraPersonaNoSoltera = personas.FirstOrDefault(p => !p.Soltero); 40 | 41 | // Sintaxis de queries 42 | var primeraPersonaNoSoltera_2 = (from p in personas 43 | where !p.Soltero 44 | select p).FirstOrDefault(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/LastDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class LastDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 45, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 24, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | var ultimaPersona = personas.Last(); 22 | var ultimaPersona2 = personas.LastOrDefault(); 23 | var ultimaPersonaSoltera = personas.Last(p => p.Soltero); 24 | 25 | // Sintaxis de queries 26 | var ultimaPersonaSoltera_2 = (from p in personas 27 | where p.Soltero 28 | select p).Last(); 29 | 30 | var a = 1; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/OfTypeDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class OfTypeDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var listado = new List() { "Felipe", 1, 2, "Claudia", true }; 14 | 15 | var strings = listado.OfType(); 16 | var numeros = listado.OfType(); 17 | 18 | // sintaxis de queries 19 | var strings_2 = from s in listado.OfType() 20 | select s; 21 | 22 | // Ejemplo 2: herencia 23 | 24 | var listadoAnimales = new List() 25 | { 26 | new Perro(){Nombre = "Firulais"}, 27 | new Gato(){Nombre = "Félix"} 28 | }; 29 | 30 | var perros = listadoAnimales.OfType(); 31 | } 32 | 33 | public abstract class Animal 34 | { 35 | public abstract string? Nombre { get; set; } 36 | } 37 | 38 | public class Perro : Animal 39 | { 40 | public override string? Nombre { get; set; } 41 | } 42 | 43 | public class Gato : Animal 44 | { 45 | public override string? Nombre { get; set; } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/SingleDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class SingleDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | 14 | var personas = new List() { 15 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 16 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 17 | new Persona { Nombre = "Alejandro", Edad = 45, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 18 | new Persona { Nombre = "Valentina", Edad = 24, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 19 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 20 | }; 21 | 22 | var personasMayorDe60 = personas.Single(p => p.Edad > 60); 23 | 24 | // Sintaxis de queries 25 | var personaMayorDe60_2 = (from p in personas 26 | where p.Edad > 60 27 | select p).Single(); 28 | 29 | try 30 | { 31 | var personaMayorA100 = personas.Single(p => p.Edad > 100); 32 | } 33 | catch (Exception ex) 34 | { 35 | Console.WriteLine("Hubo un error, arreglo vacío"); 36 | } 37 | 38 | try 39 | { 40 | var personaMayor5 = personas.SingleOrDefault(p => p.Edad > 5); 41 | } 42 | catch (Exception ex) 43 | { 44 | Console.WriteLine("Hubo otro error, arreglo con mas de un elemento"); 45 | } 46 | 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/WhereDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class WhereDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | int[] numeros = Enumerable.Range(1, 20).ToArray(); 14 | 15 | var numerosImpares = numeros.Where(n => n % 2 == 1).ToList(); 16 | 17 | //Console.WriteLine("Los numeros impares son:"); 18 | //foreach (var numero in numerosImpares) 19 | //{ 20 | // Console.WriteLine(numero); 21 | //} 22 | 23 | // Sintaxis de métodos 24 | var numerosImparesMayoresQue10 = numeros.Where(n => n % 2 == 1 && n > 10).ToList(); 25 | 26 | // Sintaxis de query 27 | var numerosImparesMayoresQue10_2 = from n in numeros 28 | where n % 2 == 1 && n > 10 29 | select n; 30 | 31 | Console.WriteLine("Los números impares mayores que 10 son:"); 32 | foreach (var numero in numerosImparesMayoresQue10_2) 33 | { 34 | Console.WriteLine(numero); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 2/WhereObjeto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_2 8 | { 9 | public class WhereObjeto 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 45, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 24, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | //var personasDe25AñosOMenos = personas.Where(p => p.Edad <= 25).ToList(); 22 | 23 | //foreach (var persona in personasDe25AñosOMenos) 24 | //{ 25 | // Console.WriteLine($"{persona.Nombre} tiene {persona.Edad} años."); 26 | //} 27 | 28 | //var solteros = personas.Where(p => p.Edad <= 25 && p.Soltero).ToList(); 29 | 30 | //foreach (var persona in solteros) 31 | //{ 32 | // Console.WriteLine($"{persona.Nombre} es soltero/a"); 33 | //} 34 | 35 | var personasConMenosDe3MesesEnLaEmpresa = personas 36 | .Where(p => p.FechaIngresoALaEmpresa >= DateTime.Now.AddMonths(-3)).ToList(); 37 | 38 | // Sintaxis de queries 39 | var personasConMenosDe3MesesEnLaEmpresa_2 = from p in personas 40 | where p.FechaIngresoALaEmpresa >= DateTime.Now.AddMonths(-3) 41 | select p; 42 | 43 | foreach (var persona in personasConMenosDe3MesesEnLaEmpresa_2) 44 | { 45 | Console.WriteLine($"{persona.Nombre} tiene menos de 3 meses en la empresa."); 46 | } 47 | 48 | 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 3/OrderByDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_3 8 | { 9 | public class OrderByDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 45, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 24, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | //var personasOrdenadasPorEdad = personas.OrderByDescending(x => x.Edad); 22 | 23 | //foreach (var persona in personasOrdenadasPorEdad) 24 | //{ 25 | // Console.WriteLine($"{persona.Nombre} tiene {persona.Edad} años de edad"); 26 | //} 27 | 28 | int[] numeros = { 1, 5, 12, 2, 3, 111, 6 }; 29 | 30 | foreach (var numero in numeros.OrderBy(x => x)) 31 | { 32 | Console.WriteLine(numero); 33 | } 34 | 35 | // Sintaxis de queries 36 | var numeros_2 = from n in numeros 37 | orderby n 38 | select n; 39 | 40 | var personasOrdenadasPorEdad_2 = from p in personas 41 | orderby p.Edad descending 42 | select p; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 3/ReverseDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_3 8 | { 9 | public class ReverseDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | 22 | var numeros = Enumerable.Range(1, 20).Reverse(); 23 | 24 | // sintaxis de queries 25 | var numeros_2 = from n in Enumerable.Range(1, 20).Reverse() 26 | select n; 27 | 28 | //personas.Reverse(); 29 | 30 | var personasInvertido = personas.AsEnumerable().Reverse().ToList(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 3/ThenByDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_3 8 | { 9 | public class ThenByDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | var personasOrdenadasPorEdad = personas.OrderBy(x => x.Edad).ThenByDescending(x => x.Nombre); 22 | 23 | foreach (var persona in personasOrdenadasPorEdad) 24 | { 25 | Console.WriteLine($"{persona.Nombre} tiene {persona.Edad} años de edad"); 26 | } 27 | 28 | // Sintaxis de queries 29 | 30 | var personasOrdenadasPorEdad_2 = from p in personas 31 | orderby p.Edad, p.Nombre descending 32 | select p; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 4/SelectDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_4 8 | { 9 | public class SelectDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 30, FechaIngresoALaEmpresa = new DateTime(2021, 1, 2), Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 19, FechaIngresoALaEmpresa = new DateTime(2015, 11, 22), Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 25, FechaIngresoALaEmpresa = new DateTime(2020, 4, 12), Soltero = false }, 17 | new Persona { Nombre = "Valentina", Edad = 37, FechaIngresoALaEmpresa = new DateTime(2021, 7, 8), Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 61, FechaIngresoALaEmpresa = DateTime.Now.AddDays(-1), Soltero = false }, 19 | }; 20 | 21 | var nombres = personas.Select(p => p.Nombre).ToList(); 22 | 23 | var nombresYEdades = personas.Select(p => new { Nombre = p.Nombre, Edad = p.Edad }).ToList(); 24 | 25 | var personasDTOs = personas.Select(p => new PersonaDTO { Nombre = p.Nombre, Edad = p.Edad }).ToList(); 26 | 27 | var numeros = Enumerable.Range(1, 5).ToList(); 28 | var numerosDuplicados = numeros.Select(n => 2 * n).ToList(); 29 | 30 | var personasConIndice = personas.Select((p, indice) => new { Persona = p, Indice = indice + 1 }).ToList(); 31 | 32 | foreach (var item in personasConIndice) 33 | { 34 | Console.WriteLine($"{item.Indice}) {item.Persona.Nombre}"); 35 | } 36 | 37 | // Sintaxis de queries 38 | var nombres_2 = (from p in personas 39 | select p.Nombre).ToList(); 40 | 41 | var nombresYEdades_2 = from p in personas 42 | select new { Nombre = p.Nombre, Edad = p.Edad }; 43 | 44 | var numerosDuplicados_2 = from n in numeros 45 | select 2 * n; 46 | 47 | // No podemos hacer el ejemplo del índice con sintaxis de queries 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 4/SelectManyDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_4 8 | { 9 | public class SelectManyDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Telefonos = { "123-456", "789-852" } }, 15 | new Persona { Nombre = "Nidia", Telefonos = { "998-478", "568-222" } }, 16 | new Persona { Nombre = "Alejandro", Telefonos = { "712-132" } }, 17 | new Persona { Nombre = "Valentina" } 18 | }; 19 | 20 | var telefonos = personas.SelectMany(x => x.Telefonos).ToList(); 21 | 22 | // Ejemplo 2: Entendiendo SelectMany con dos colecciones diferentes 23 | int[] numeros = { 1, 2, 3 }; 24 | 25 | var personasYNumeros = personas.SelectMany(p => numeros, (persona, numero) => new 26 | { 27 | Persona = persona, 28 | Numero = numero 29 | }); 30 | 31 | foreach (var item in personasYNumeros) 32 | { 33 | //Console.WriteLine($"{item.Persona.Nombre} - {item.Numero}"); 34 | } 35 | 36 | // Ejemplo 3: personas y telefonos 37 | 38 | var personasYTelefonos = personas.SelectMany(p => p.Telefonos, (persona, telefono) => new 39 | { 40 | Persona = persona, 41 | Telefono = telefono 42 | }); 43 | 44 | foreach (var item in personasYTelefonos) 45 | { 46 | Console.WriteLine($"{item.Persona.Nombre} - {item.Telefono}"); 47 | } 48 | 49 | // Sintaxis de queries 50 | var telefonos_2 = from p in personas 51 | from telefono in p.Telefonos 52 | select telefono; 53 | 54 | var personasYNumeros_2 = from p in personas 55 | from n in numeros 56 | select new 57 | { 58 | Persona = p, 59 | Numero = n 60 | }; 61 | 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 5/Agregado.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_5 8 | { 9 | public class Agregado 10 | { 11 | public void Ejemplo() 12 | { 13 | var numeros = Enumerable.Range(1, 5); 14 | 15 | var resultado = numeros.Aggregate((a, b) => a * b); // 1x2x3x4x5 16 | 17 | Console.WriteLine("Resultado es " + resultado); 18 | 19 | var resultadoConValorInicial = numeros.Aggregate(10, (a, b) => a * b); 20 | 21 | Console.WriteLine("El resultado con el valor semilla es: " + resultadoConValorInicial); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 5/Conteo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_5 8 | { 9 | public class Conteo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Soltero = true }, 15 | new Persona { Nombre = "Nidia", Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Soltero = true }, 17 | new Persona { Nombre = "Valentina", Soltero = false } 18 | }; 19 | 20 | Console.WriteLine($"La cantidad de personas es {personas.Count()}"); 21 | 22 | Console.WriteLine($"La cantidad de personas solteras es {personas.Count(p => p.Soltero)}"); 23 | 24 | Console.WriteLine($"Int max = {int.MaxValue.ToString("N")}"); 25 | 26 | //personas.LongCount(); 27 | 28 | // Sintaxis de queries (no muy útil en estos casos) 29 | 30 | var conteoSolteros = (from p in personas 31 | where p.Soltero 32 | select p).Count(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 5/MaxByMinBy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_5 8 | { 9 | public class MaxByMinBy 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 19, }, 15 | new Persona { Nombre = "Nidia", Edad = 25 }, 16 | new Persona { Nombre = "Alejandro", Edad = 30 }, 17 | new Persona { Nombre = "Valentina", Edad = 22 } 18 | }; 19 | 20 | var numeros = Enumerable.Range(1, 5); 21 | 22 | Console.WriteLine($"La suma de los numeros es {numeros.Sum()}"); 23 | Console.WriteLine($"La suma de los numeros es {personas.Sum(p => p.Edad)}"); 24 | 25 | Console.WriteLine($"La edad máxima de las personas es {personas.Max(x => x.Edad)}"); 26 | Console.WriteLine($"La edad mínima de las personas es {personas.Min(x => x.Edad)}"); 27 | 28 | var personaDeMayorEdad = personas.MaxBy(x => x.Edad); 29 | var personaDeMenorEdad = personas.MinBy(x => x.Edad); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 5/Promedio.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_5 8 | { 9 | public class Promedio 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 19, }, 15 | new Persona { Nombre = "Nidia", Edad = 25 }, 16 | new Persona { Nombre = "Alejandro", Edad = 30 }, 17 | new Persona { Nombre = "Valentina", Edad = 22 } 18 | }; 19 | 20 | var numeros = Enumerable.Range(1, 5); 21 | 22 | Console.WriteLine($"El promedio de los números es {numeros.Average()}"); 23 | 24 | Console.WriteLine($"El promedio de las edades es {personas.Average(p => p.Edad)}"); 25 | 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 5/SumMaxMin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_5 8 | { 9 | public class SumMaxMin 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", Edad = 19, }, 15 | new Persona { Nombre = "Nidia", Edad = 25 }, 16 | new Persona { Nombre = "Alejandro", Edad = 30 }, 17 | new Persona { Nombre = "Valentina", Edad = 22 } 18 | }; 19 | 20 | var numeros = Enumerable.Range(1, 5); 21 | 22 | Console.WriteLine($"La suma de los numeros es {numeros.Sum()}"); 23 | Console.WriteLine($"La suma de los numeros es {personas.Sum(p => p.Edad)}"); 24 | 25 | Console.WriteLine($"La edad máxima de las personas es {personas.Max(x => x.Edad)}"); 26 | Console.WriteLine($"La edad mínima de las personas es {personas.Min(x => x.Edad)}"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 6/AllDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_6 8 | { 9 | public class AllDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo",Edad = 19, Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 25, Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 30, Soltero = true }, 17 | new Persona { Nombre = "Valentina", Edad = 22, Soltero = false } 18 | }; 19 | 20 | var sonTodasLasPersonasMayoresDeEdad = personas.All(p => p.Edad >= 18); 21 | 22 | var sonTodosSolteros = personas.All(p => p.Soltero); 23 | 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 6/AnyDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_6 8 | { 9 | public class AnyDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo",Edad = 19, Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 25, Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 30, Soltero = true }, 17 | new Persona { Nombre = "Valentina", Edad = 22, Soltero = false } 18 | }; 19 | 20 | var existeMenor = personas.Any(p => p.Edad < 18); 21 | 22 | var existePersonaMayorDe20 = personas.Any(p => p.Edad > 20); 23 | 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 6/ContainsDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_6 8 | { 9 | public class ContainsDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var numeros = Enumerable.Range(1, 5); 14 | 15 | var estaElNumero3 = numeros.Contains(3); 16 | 17 | var estaElNumero20 = numeros.Contains(20); 18 | 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 7/Paginacion.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_7 8 | { 9 | public class Paginacion 10 | { 11 | public void Ejemplo() 12 | { 13 | var numeros = Enumerable.Range(1, 100); 14 | 15 | for (int i = 1; i <= 10; i++) 16 | { 17 | Console.WriteLine($"Página {i}"); 18 | var paginado = numeros.Paginar(i, 10); 19 | foreach (var item in paginado) 20 | { 21 | Console.WriteLine(item); 22 | } 23 | } 24 | } 25 | } 26 | 27 | public static class IEnumerableExtensions 28 | { 29 | public static IEnumerable Paginar(this IEnumerable coleccion, int pagina, int tamañoLote) 30 | { 31 | return coleccion.Skip((pagina - 1) * tamañoLote).Take(tamañoLote); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 7/SkipYSkipLast.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_7 8 | { 9 | public class SkipYSkipLast 10 | { 11 | public void Ejemplo() 12 | { 13 | var numeros = Enumerable.Range(1, 100); 14 | 15 | var primeros10Numeros = numeros.Take(10).ToList(); 16 | var segundoLoteDe10 = numeros.Skip(10).Take(10).ToList(); 17 | 18 | var ultimos10Numeros = numeros.TakeLast(10).ToList(); 19 | var penultimoLote = numeros.SkipLast(10).TakeLast(10).ToList(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 7/TakeWhileSkipWhile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_7 8 | { 9 | public class TakeWhileSkipWhile 10 | { 11 | public void Ejemplo() 12 | { 13 | int[] numeros = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 2, 1 }; 14 | 15 | // Tomar los elementos mientras el predicado sea true. 16 | // A partir de que el predicado sea falso, dejar de tomar elementos. 17 | var resultadoTakeWhile = numeros.TakeWhile(x => x < 5).ToList(); 18 | 19 | // Saltarse los elementos mientras el predicado sea true. 20 | // A partir de que el predicado sea falso, tomar todo lo que sigue. 21 | var resultadoSkipWhile = numeros.SkipWhile(x => x < 5).ToList(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 7/TakeYTakeLast.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_7 8 | { 9 | public class TakeYTakeLast 10 | { 11 | public void Ejemplo() 12 | { 13 | var numeros = Enumerable.Range(1, 100); 14 | 15 | var primeros10Numeros = numeros.Take(10).ToList(); 16 | 17 | var ultimos10Numeros = numeros.TakeLast(10).ToList(); 18 | 19 | // Sintaxis de queries 20 | var primeros10Numeros_2 = (from n in numeros 21 | select n).Take(10); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 8/GroupByDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_8 8 | { 9 | public class GroupByDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | 14 | var personas = new List() { 15 | new Persona { Nombre = "Eduardo",Edad = 19, Soltero = true }, 16 | new Persona { Nombre = "Nidia", Edad = 25, Soltero = true }, 17 | new Persona { Nombre = "Alejandro", Edad = 30, Soltero = true }, 18 | new Persona { Nombre = "Valentina", Edad = 17, Soltero = false }, 19 | new Persona { Nombre = "Roberto", Edad = 18, Soltero = true }, 20 | new Persona { Nombre = "Eugenia", Edad = 27, Soltero = false }, 21 | new Persona { Nombre = "Esmerlin", Edad = 45, Soltero = false } 22 | }; 23 | 24 | var agrupamientoPorSolteria = personas.GroupBy(p => p.Soltero); 25 | 26 | // sintaxis de queries 27 | var agrupamientoPorSolteria_2 = from p in personas 28 | group p by p.Soltero into grupos 29 | select grupos; 30 | 31 | foreach (var item in agrupamientoPorSolteria_2) 32 | { 33 | Console.WriteLine($"Grupo de las personas donde Soltero = {item.Key} (Total: {item.Count()})"); 34 | foreach (var persona in item) 35 | { 36 | Console.WriteLine($"- {persona.Nombre}"); 37 | } 38 | } 39 | 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 8/GroupByEdad.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_8 8 | { 9 | public class GroupByEdad 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo",Edad = 19, Soltero = true }, 15 | new Persona { Nombre = "Nidia", Edad = 25, Soltero = true }, 16 | new Persona { Nombre = "Alejandro", Edad = 30, Soltero = true }, 17 | new Persona { Nombre = "Valentina", Edad = 17, Soltero = false }, 18 | new Persona { Nombre = "Roberto", Edad = 18, Soltero = true }, 19 | new Persona { Nombre = "Eugenia", Edad = 27, Soltero = false }, 20 | new Persona { Nombre = "Esmerlin", Edad = 45, Soltero = false } 21 | }; 22 | 23 | var agrupamientoPorRangoEdad = personas.GroupBy(p => p.Edad / 5); 24 | 25 | // Sintaxis de queries 26 | 27 | var agrupamientoPorRangoEdad_2 = from p in personas 28 | group p by p.Edad / 5 into grupos 29 | select grupos; 30 | 31 | foreach (var item in agrupamientoPorRangoEdad_2) 32 | { 33 | Console.WriteLine($"Grupo de las personas en el rango de edad {item.Key * 5} - {item.Key * 5 + 5 - 1}"); 34 | 35 | foreach (var persona in item) 36 | { 37 | Console.WriteLine($"- {persona.Nombre} (edad: {persona.Edad})"); 38 | } 39 | } 40 | 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 9/GroupJoinDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_9 8 | { 9 | public class GroupJoinDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 15 | new Persona { Nombre = "Nidia", EmpresaId = 1 }, 16 | new Persona { Nombre = "Alejandro", EmpresaId = 3 }, 17 | new Persona { Nombre = "Valentina", EmpresaId = 2 }, 18 | new Persona { Nombre = "Roberto", EmpresaId = 3 }, 19 | new Persona { Nombre = "Eugenia"}, 20 | new Persona { Nombre = "Esmerlin", EmpresaId = 3 } 21 | }; 22 | 23 | var empresas = new List() 24 | { 25 | new Empresa{Id = 1, Nombre = "Electrodomésticos Felipe"}, 26 | new Empresa{Id = 2, Nombre = "Bicicletas Valentina"}, 27 | new Empresa{Id = 3, Nombre = "Gimnasio Esmerlin"}, 28 | new Empresa{Id = 4, Nombre = "Ferreteria Lorenzo"} 29 | }; 30 | 31 | var empresasYSusEmpleados = empresas.GroupJoin(personas, e => e.Id, p => p.EmpresaId, 32 | (empresa, personas) => new { Empresa = empresa, Personas = personas }); 33 | 34 | // Sintaxis de queries 35 | var empresasYSusEmpleados_2 = from empresa in empresas 36 | join persona in personas 37 | on empresa.Id equals persona.EmpresaId into personas2 38 | select new 39 | { 40 | Empresa = empresa, 41 | Personas = personas2 42 | }; 43 | 44 | foreach (var item in empresasYSusEmpleados_2) 45 | { 46 | Console.WriteLine($"Las siguientes personas trabajan en {item.Empresa.Nombre}"); 47 | 48 | foreach (var persona in item.Personas) 49 | { 50 | Console.WriteLine($"-{persona.Nombre}"); 51 | } 52 | } 53 | 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /CursoLINQ/Modulo 9/JoinDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ.Modulo_9 8 | { 9 | public class JoinDemo 10 | { 11 | public void Ejemplo() 12 | { 13 | var personas = new List() { 14 | new Persona { Nombre = "Eduardo", EmpresaId = 1, }, 15 | new Persona { Nombre = "Nidia", EmpresaId = 1 }, 16 | new Persona { Nombre = "Alejandro", EmpresaId = 3 }, 17 | new Persona { Nombre = "Valentina", EmpresaId = 2 }, 18 | new Persona { Nombre = "Roberto", EmpresaId = 3 }, 19 | new Persona { Nombre = "Eugenia"}, 20 | new Persona { Nombre = "Esmerlin", EmpresaId = 3 } 21 | }; 22 | 23 | var empresas = new List() 24 | { 25 | new Empresa{Id = 1, Nombre = "Electrodomésticos Felipe"}, 26 | new Empresa{Id = 2, Nombre = "Bicicletas Valentina"}, 27 | new Empresa{Id = 3, Nombre = "Gimnasio Esmerlin"} 28 | }; 29 | 30 | var personasYEmpresas = personas.Join(empresas, p => p.EmpresaId, e => e.Id, (persona, empresa) => new 31 | { 32 | Persona = persona, 33 | Empresa = empresa 34 | }); 35 | 36 | // Sintaxis de queries 37 | var personasYEmpresas_2 = from persona in personas 38 | join empresa in empresas on persona.EmpresaId equals empresa.Id 39 | select new 40 | { 41 | Persona = persona, 42 | Empresa = empresa 43 | }; 44 | 45 | foreach (var item in personasYEmpresas_2) 46 | { 47 | Console.WriteLine($"{item.Persona.Nombre} trabaja en {item.Empresa.Nombre}"); 48 | } 49 | 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /CursoLINQ/Persona.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CursoLINQ 9 | { 10 | [DebuggerDisplay("{Nombre}")] 11 | public class Persona 12 | { 13 | public string Nombre { get; set; } 14 | public int Edad { get; set; } 15 | public bool Soltero { get; set; } 16 | public DateTime FechaIngresoALaEmpresa { get; set; } 17 | public List Telefonos = new List(); 18 | public int EmpresaId { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CursoLINQ/PersonaDTO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CursoLINQ 8 | { 9 | public class PersonaDTO 10 | { 11 | public string Nombre { get; set; } 12 | public int Edad { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CursoLINQ/Program.cs: -------------------------------------------------------------------------------- 1 | using CursoLINQ; 2 | 3 | Console.WriteLine("El código de ejemplo se encuentra en las respectivas carpetas"); -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31606.5 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EjemploEFCore", "EjemploEFCore\EjemploEFCore.csproj", "{1CEEDC68-15BB-4092-9B58-11F705907D15}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {1CEEDC68-15BB-4092-9B58-11F705907D15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {1CEEDC68-15BB-4092-9B58-11F705907D15}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {1CEEDC68-15BB-4092-9B58-11F705907D15}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {1CEEDC68-15BB-4092-9B58-11F705907D15}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {E1EA2987-45D5-40FA-B6A0-43E46A9D619B} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/ApplicationDbContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace EjemploEFCore 9 | { 10 | public class ApplicationDbContext: DbContext 11 | { 12 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 13 | { 14 | //optionsBuilder.UseSqlite($"Data Source=cursoLINQ.db"); 15 | optionsBuilder.UseSqlServer("Server=.;Database=CursoLINQ;Trusted_Connection=True"); 16 | } 17 | 18 | public DbSet Personas { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/EjemploEFCore.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/Migrations/20210817150212_Initial.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using EjemploEFCore; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace EjemploEFCore.Migrations 10 | { 11 | [DbContext(typeof(ApplicationDbContext))] 12 | [Migration("20210817150212_Initial")] 13 | partial class Initial 14 | { 15 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 16 | { 17 | #pragma warning disable 612, 618 18 | modelBuilder 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("ProductVersion", "6.0.0-preview.7.21378.4") 21 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 22 | 23 | modelBuilder.Entity("EjemploEFCore.Persona", b => 24 | { 25 | b.Property("Id") 26 | .ValueGeneratedOnAdd() 27 | .HasColumnType("int") 28 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 29 | 30 | b.Property("Nombre") 31 | .IsRequired() 32 | .HasColumnType("nvarchar(max)"); 33 | 34 | b.HasKey("Id"); 35 | 36 | b.ToTable("Personas"); 37 | }); 38 | #pragma warning restore 612, 618 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/Migrations/20210817150212_Initial.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace EjemploEFCore.Migrations 4 | { 5 | public partial class Initial : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.CreateTable( 10 | name: "Personas", 11 | columns: table => new 12 | { 13 | Id = table.Column(type: "int", nullable: false) 14 | .Annotation("SqlServer:Identity", "1, 1"), 15 | Nombre = table.Column(type: "nvarchar(max)", nullable: false) 16 | }, 17 | constraints: table => 18 | { 19 | table.PrimaryKey("PK_Personas", x => x.Id); 20 | }); 21 | } 22 | 23 | protected override void Down(MigrationBuilder migrationBuilder) 24 | { 25 | migrationBuilder.DropTable( 26 | name: "Personas"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/Migrations/ApplicationDbContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using EjemploEFCore; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | 8 | namespace EjemploEFCore.Migrations 9 | { 10 | [DbContext(typeof(ApplicationDbContext))] 11 | partial class ApplicationDbContextModelSnapshot : ModelSnapshot 12 | { 13 | protected override void BuildModel(ModelBuilder modelBuilder) 14 | { 15 | #pragma warning disable 612, 618 16 | modelBuilder 17 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 18 | .HasAnnotation("ProductVersion", "6.0.0-preview.7.21378.4") 19 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 20 | 21 | modelBuilder.Entity("EjemploEFCore.Persona", b => 22 | { 23 | b.Property("Id") 24 | .ValueGeneratedOnAdd() 25 | .HasColumnType("int") 26 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 27 | 28 | b.Property("Nombre") 29 | .IsRequired() 30 | .HasColumnType("nvarchar(max)"); 31 | 32 | b.HasKey("Id"); 33 | 34 | b.ToTable("Personas"); 35 | }); 36 | #pragma warning restore 612, 618 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/Persona.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace EjemploEFCore 8 | { 9 | public class Persona 10 | { 11 | public int Id { get; set; } 12 | public string Nombre { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/Program.cs: -------------------------------------------------------------------------------- 1 | // See https://aka.ms/new-console-template for more information 2 | 3 | using EjemploEFCore; 4 | 5 | using (var context = new ApplicationDbContext()) 6 | { 7 | var persona1 = new Persona() { Nombre = "Felipe" }; 8 | var persona2 = new Persona() { Nombre = "Claudia" }; 9 | 10 | context.AddRange(persona1, persona2); 11 | context.SaveChanges(); 12 | } 13 | 14 | using (var context = new ApplicationDbContext()) 15 | { 16 | var personas = context.Personas.ToList(); 17 | var personasNombre = context.Personas.Select(p => p.Nombre).ToList(); 18 | var felipe = context.Personas.FirstOrDefault(p => p.Nombre == "Felipe"); 19 | var guillermo = context.Personas.FirstOrDefault(p => p.Nombre == "Guillermo"); 20 | 21 | // Sintaxis de queries 22 | var felipe_2 = (from p in context.Personas 23 | where p.Nombre == "Felipe" 24 | select p).FirstOrDefault(); 25 | 26 | //var segmentos = context.Personas.Chunk(3).ToList(); 27 | } 28 | 29 | Console.WriteLine("Hello, World!"); 30 | -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "EjemploEFCore": { 4 | "commandName": "Project", 5 | "workingDirectory": "C:\\Users\\ASUS\\source\\repos\\EjemploEFCore\\EjemploEFCore" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /EjemploEFCore/EjemploEFCore/cursoLINQ.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavilanch/Curso-LINQ/519b4b5175cb6f5242acc7897d9284a2309af3dc/EjemploEFCore/EjemploEFCore/cursoLINQ.db --------------------------------------------------------------------------------