You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.5 KiB

2 years ago
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}");
}
2 years ago
Console.WriteLine("first, single");
2 years ago
var firstItem = products.First(item => item.ProductId == 3);
var singleItem = products.Single(item => item.ProductId == 3);
Console.WriteLine($"first == single: {firstItem == singleItem}");
}
}
}