From 8a3eb1fe6fd28794bfe3a484c3562549621f8eb1 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 3 Aug 2023 13:23:50 +0900 Subject: [PATCH] linq --- .../LinqInNotIn/LinqInNotIn.csproj | 10 +++++ PacticeSolution/LinqInNotIn/Product.cs | 20 +++++++++ PacticeSolution/LinqInNotIn/Program.cs | 42 +++++++++++++++++++ PacticeSolution/PacticeSolution.sln | 6 +++ 4 files changed, 78 insertions(+) create mode 100644 PacticeSolution/LinqInNotIn/LinqInNotIn.csproj create mode 100644 PacticeSolution/LinqInNotIn/Product.cs create mode 100644 PacticeSolution/LinqInNotIn/Program.cs diff --git a/PacticeSolution/LinqInNotIn/LinqInNotIn.csproj b/PacticeSolution/LinqInNotIn/LinqInNotIn.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/PacticeSolution/LinqInNotIn/LinqInNotIn.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/PacticeSolution/LinqInNotIn/Product.cs b/PacticeSolution/LinqInNotIn/Product.cs new file mode 100644 index 0000000..bf74f27 --- /dev/null +++ b/PacticeSolution/LinqInNotIn/Product.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LinqInNotIn +{ + internal class Product + { + public int ProductId { get; set; } + public string Name { get; set; } + + public Product(int id, string name) + { + this.ProductId = id; + this.Name = name; + } + } +} diff --git a/PacticeSolution/LinqInNotIn/Program.cs b/PacticeSolution/LinqInNotIn/Program.cs new file mode 100644 index 0000000..64f5da4 --- /dev/null +++ b/PacticeSolution/LinqInNotIn/Program.cs @@ -0,0 +1,42 @@ +namespace LinqInNotIn +{ + internal class Program + { + static void Main(string[] args) + { + List products = new List(); + products.Add(new Product(1, "Scissors")); + products.Add(new Product(2, "Pencil")); + products.Add(new Product(3, "Ballpen")); + //products.Add(new Product(3, "Null Ballpen")); + products.Add(new Product(4, "Snack")); + products.Add(new Product(5, "Beverage")); + + int[] ids = { 2, 4, 5 }; + + var inQuery = from item in products + where ids.Contains(item.ProductId) + select item; + + var notInQuery = from item in products + where !ids.Contains(item.ProductId) + select item; + + Console.WriteLine("inQuery"); + foreach (var item in inQuery) + { + Console.WriteLine($"idx: {item.ProductId}, name: {item.Name}"); + } + + Console.WriteLine("notInQuery"); + foreach (var item in notInQuery) + { + Console.WriteLine($"idx: {item.ProductId}, name: {item.Name}"); + } + + var firstItem = products.First(item => item.ProductId == 3); + var singleItem = products.Single(item => item.ProductId == 3); + Console.WriteLine($"first == single: {firstItem == singleItem}"); + } + } +} \ No newline at end of file diff --git a/PacticeSolution/PacticeSolution.sln b/PacticeSolution/PacticeSolution.sln index 93d6085..2143681 100644 --- a/PacticeSolution/PacticeSolution.sln +++ b/PacticeSolution/PacticeSolution.sln @@ -121,6 +121,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataTemplateSample", "DataT EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandSample", "CommandSample\CommandSample.csproj", "{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqInNotIn", "LinqInNotIn\LinqInNotIn.csproj", "{9261D8E1-5DEE-4329-AB32-946AD5E69C66}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -363,6 +365,10 @@ Global {AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Debug|Any CPU.Build.0 = Debug|Any CPU {AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Release|Any CPU.ActiveCfg = Release|Any CPU {AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Release|Any CPU.Build.0 = Release|Any CPU + {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE