linq
This commit is contained in:
10
PacticeSolution/LinqInNotIn/LinqInNotIn.csproj
Normal file
10
PacticeSolution/LinqInNotIn/LinqInNotIn.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
20
PacticeSolution/LinqInNotIn/Product.cs
Normal file
20
PacticeSolution/LinqInNotIn/Product.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
42
PacticeSolution/LinqInNotIn/Program.cs
Normal file
42
PacticeSolution/LinqInNotIn/Program.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace LinqInNotIn
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
List<Product> products = new List<Product>();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user