LINQ `Except` অপারেটর সেট অপারেটর ক্যাটেগরির অধীনে আয়ত্ত।
`Except()` মেথড دوটি কালেকশন নেয় এবং সেগুলো খুঁজে বের করে যেগুলো দ্বিতীয় কালেকশনে উপস্থিত নয়।
`Except` এক্সটেনশন মেথড কমপ্লেক্স টাইপের কালেকশনের জন্য সঠিক রেজাল্ট রিটার্ন করে না।
Except() মেথড ব্যবহার করে উদাহরণ
উদাহরণ
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
List animalsList1 = new List { "tiger", "lion", "dog" };
Console.WriteLine("Values in List1:");
foreach (var val in animalsList1) { Console.WriteLine(val); }
List animalsList2 = new List { "tiger", "cat", "deer" };
Console.WriteLine("Values in List2:");
foreach (var val in animalsList2) { Console.WriteLine(val); }
var animalsList3 = animalsList1.Except(animalsList2);
Console.WriteLine("Value in List1 that are not in List2:");
foreach (var val in animalsList3) { Console.WriteLine(val); }
Console.ReadLine();
}
}
}
আউটপুট
Values in List1: tiger lion dog
Values in List2: tiger cat deer
Value in List1 that are not in List2: lion dog
Where Clause ব্যবহার করে উদাহরণ (কমপ্লেক্স টাইপের জন্য)
উদাহরণ
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
List fruitsList1 = new List
{
new Fruit { Name = "Apple", Size = "Small" },
new Fruit { Name = "Orange", Size = "Small" }
};
Console.WriteLine("Values in List1:");
foreach (var val in fruitsList1) { Console.WriteLine(val.Name); }
List fruitsList2 = new List
{
new Fruit { Name = "Apple", Size = "Small" },
new Fruit { Name = "Mango", Size = "Small" }
};
Console.WriteLine("Values in List2:");
foreach (var val in fruitsList2) { Console.WriteLine(val.Name); }
var fruitsList3 = fruitsList1.Where(f1 => fruitsList2.All(f2 => f2.Name != f1.Name));
Console.WriteLine("Values in List1 that are not in List2:");
foreach (var val in fruitsList3) { Console.WriteLine(val.Name); }
Console.ReadLine();
}
}
public class Fruit
{
public string Name { get; set; }
public string Size { get; set; }
}
}
আউটপুট
Values in List1: Apple Orange
Values in List2: Apple Mango
Values in List1 that are not in List2: Orange