Contains & Except
===================================================================
Now lets make some twist
Code
Output
====================================================================
Lets try with a List of Classes
ANY
List<int> num1 = new List<int> { 1,4,7,12};
List<int> num2 = new List<int> { 1, 9, 7, 13 };
List<int> final3 = num2.Where(x => !num1.Any(y=>y==x)).ToList();//9 13
List<int> num1 = new List<int> { 1,4,7,12};
List<int> num2 = new List<int> { 1, 9, 7, 13,13 };
List<int> final = num2.Where(x => !num1.Contains(x)).ToList();//9 13 13
List<int> finalAny = num2.Where(x => !num1.Any(y => y == x)).ToList();// 9 13 13
List<int> final = num2.Where(x => !num1.Any(y=>y==x)).Distinct().ToList();// 9 13
=====================================================================
I hope you enjoyed the things :)
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqDetails
{
class Program
{
static void Main(string[] args)
{
List<int> num1 = new List<int> { 1,4,7,12};
List<int> num2 = new List<int> { 1, 9, 7, 13 };
List<int> final = num2.Where(x => !num1.Contains(x)).ToList();//9 13
foreach (var items in final)
{
Console.WriteLine(items);
}
Console.WriteLine("=======Except Begins=======");
List<int> final1 = num2.Except(num1).ToList();//9 13
foreach (var items in final1)
{
Console.WriteLine(items);
}
Console.ReadLine();
}
}
- Both output is 9 and 13
- First One is Not In operator :- x not there in num1.
- Second one is Except
===================================================================
Now lets make some twist
Code
class Program
{
static void Main(string[] args)
{
List<int> num1 = new List<int> { 1,4,7,12};
List<int> num2 = new List<int> { 1, 9, 7, 13,13 };
List<int> final = num2.Where(x => !num1.Contains(x)).ToList();//9 13 13
foreach (var items in final)
{
Console.WriteLine(items);
}
Console.WriteLine("=======Except Begins=======");
List<int> final1 = num2.Except(num1).ToList();//9 13
foreach (var items in final1)
{
Console.WriteLine(items);
}
Console.ReadLine();
}
Output
- Except always gives distinct values
====================================================================
Lets try with a List of Classes
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqDetails
{
class list1
{
public string FirstName { get; set; }
public string Id { get; set; }
}
class list2
{
public string FirstName { get; set; }
public string Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<list1> list1 = new List<list1>();
List<list2> list2 = new List<list2>();
list1.Add(new list1 { FirstName="Biswa", Id="1" });
list1.Add(new list1 { FirstName = "Anurag", Id = "2" });
list1.Add(new list1 { FirstName="Prasad", Id="3" });
list2.Add(new list2 { FirstName = "Abhi", Id = "1" });
list2.Add(new list2 { FirstName = "Anurag", Id = "2" });
list2.Add(new list2 { FirstName = "Siba", Id = "3" });
List<list2> list2NotInList1 = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName)).ToList();
foreach (list2 list in list2NotInList1)
{
Console.WriteLine(list.FirstName + " " + list.Id);
}
Console.ReadLine();
}
}
}
ANY
- Using Any for the first scenario
List<int> num1 = new List<int> { 1,4,7,12};
List<int> num2 = new List<int> { 1, 9, 7, 13 };
List<int> final3 = num2.Where(x => !num1.Any(y=>y==x)).ToList();//9 13
- If duplicate is there then it takes duplicate value too.
List<int> num1 = new List<int> { 1,4,7,12};
List<int> num2 = new List<int> { 1, 9, 7, 13,13 };
List<int> final = num2.Where(x => !num1.Contains(x)).ToList();//9 13 13
List<int> finalAny = num2.Where(x => !num1.Any(y => y == x)).ToList();// 9 13 13
- To Make it distinct use below code
List<int> final = num2.Where(x => !num1.Any(y=>y==x)).Distinct().ToList();// 9 13
=====================================================================
I hope you enjoyed the things :)
No comments:
Post a Comment