Linq

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday 16 December 2014

GroupBy & Join Linq C#

Group By Linq
  • powerful feature of linq Similar to Group by as SQL
  • It can be done using both linq as well as lambda expression.
Code
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace XML  
 {  
   class Program  
   {  
     public class student  
     {  
       public int id { get; set; }  
       public string name { get; set; }  
       public int classId { get; set; }  
       public List<student> getStudents()  
       {  
         List<student> obj = new List<student>();  
         obj.Add(new student { id = 101, name = "abc1", classId = 1 });  
         obj.Add(new student { id = 102, name = "abc2", classId = 1 });  
         obj.Add(new student { id = 103, name = "abc3", classId = 2 });  
         obj.Add(new student { id = 104, name = "abc4", classId = 2 });  
         obj.Add(new student { id = 105, name = "abc5", classId = 2 });  
         obj.Add(new student { id = 106, name = "abc6", classId = 3 });  
         obj.Add(new student { id = 107, name = "abc7", classId = 3 });  
         obj.Add(new student { id = 108, name = "abc8", classId = 3 });  
         obj.Add(new student { id = 109, name = "abc9", classId = 3 });  
         return obj;  
       }  
     }  
     static void Main(string[] args)  
     {  
       student s = new student();  
       var students = s.getStudents().ToList();  
       var final = students.GroupBy(x => x.classId).ToDictionary(y => y.Key, y => y.ToList());  
       foreach (var x in final)  
       {  
         Console.WriteLine("----" + x.Key + "-----");  
         foreach (var item in x.Value)  
         {  
           Console.WriteLine(item.id);  
           Console.WriteLine(item.name);  
           Console.WriteLine();  
         }  
         Console.WriteLine("===================================");  
       }  
       Console.WriteLine("--------Slight Modification----------");  
       var final1 = students.GroupBy(x => x.classId).Select(g => new { Key=g.Key, List=g.ToList() });  
       foreach (var item in final1)  
       {  
         Console.WriteLine("----" + item.Key + "-----");  
         foreach (var item1 in item.List)  
         {  
           Console.WriteLine(item1.id);  
           Console.WriteLine(item1.name);  
           Console.WriteLine();  
         }  
       }  
       Console.ReadLine();  
     }  
   }  
 }  

Both Gives Same result: 



























Group By and Sum

Code
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace LINQ  
 {  
   class Program  
   {  
     public class student  
     {  
       public int id { get; set; }  
       public string name { get; set; }  
       public int classId { get; set; }  
       public int Salary { get; set; }  
       public List<student> getStudents()  
       {  
         List<student> obj = new List<student>();  
         obj.Add(new student { id = 101, name = "abc1", classId = 1, Salary = 100 });  
         obj.Add(new student { id = 102, name = "abc2", classId = 1, Salary = 200 });  
         obj.Add(new student { id = 103, name = "abc3", classId = 2, Salary = 300 });  
         obj.Add(new student { id = 104, name = "abc4", classId = 2, Salary = 800 });  
         obj.Add(new student { id = 105, name = "abc5", classId = 2, Salary = 900 });  
         obj.Add(new student { id = 106, name = "abc6", classId = 3, Salary = 250 });  
         obj.Add(new student { id = 107, name = "abc7", classId = 3, Salary = 260 });  
         obj.Add(new student { id = 108, name = "abc8", classId = 3, Salary = 290 });  
         return obj;  
       }  
     }  
     static void Main(string[] args)  
     {  
       student s = new student();  
       var students = s.getStudents().ToList();  
       var z = students.GroupBy(x => x.classId).Select(g => new {ClassId=g.Key, TotalSalary= g.Sum(x=>x.Salary) });  
       foreach (var item in z)  
       {  
         Console.WriteLine(item.ClassId);  
         Console.WriteLine(item.TotalSalary);  
         Console.WriteLine();  
       }  
       Console.ReadLine();  
     }  
   }  
 }  


O/P:
















using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace LINQ  
 {  
   class Program  
   {  
     class Parent  
     {  
       public int Id { get; set; }  
       public string Val { get; set; }  
     }  
     class Child  
     {  
       public int Id { get; set; }  
       public string ChildVal { get; set; }  
     }  
     static void Main(string[] args)  
     {  
       List<Parent> plist = new List<Parent>() {  
         new Parent{ Id=1, Val="X"},  
         new Parent{ Id=2, Val="Y"},  
         new Parent{ Id=3, Val="Z"}  
       };  
       List<Child> clist = new List<Child>() {  
         new Child{ Id=1, ChildVal="X1"},  
         new Child{ Id=1, ChildVal="X2"},  
         new Child{ Id=1, ChildVal="X3"},  
         new Child{ Id=2, ChildVal="Y1"},  
         new Child{ Id=2, ChildVal="Y2"},  
         new Child{ Id=4, ChildVal="Y3"},  
       };  
       var joinedResult = from p in plist  
                 join c in clist on p.Id equals c.Id  
             select new { p.Val, c.ChildVal };  
       Console.WriteLine("===JOIN===");  
       foreach (var item in joinedResult)  
       {  
         Console.WriteLine(item.Val);  
         Console.WriteLine(item.ChildVal);  
         Console.WriteLine();  
       }  
       Console.WriteLine("===GROUPJOIN===");  
       var y = from p in plist  
           join c in clist on p.Id equals c.Id into g  
           select new { Parent = p, Children = g };  
       foreach (var item in y)  
       {  
         Console.WriteLine(item.Parent.Id);  
         foreach (var item1 in item.Children)  
         {  
           Console.WriteLine(item1.ChildVal);  
         }  
         Console.WriteLine();  
       }  
       Console.ReadLine();  
     }  
   }  
 }  



Second Highest Salary using LINQ

Topic

Finding the Second Highest or nth highest using Linq

Scenario

  • List of Students. Each having name,Salary etc
  • Find the second highest or nth highest salary
Code

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace XML  
 {  
   class Program  
   {  
     public class student  
     {  
       public int id { get; set; }  
       public string name { get; set; }  
       public int classId { get; set; }  
       public int Salary { get; set; }  
       public List<student> getStudents()  
       {  
         List<student> obj = new List<student>();  
         obj.Add(new student { id = 101, name = "abc1", classId = 1, Salary = 100 });  
         obj.Add(new student { id = 102, name = "abc2", classId = 1, Salary = 200 });  
         obj.Add(new student { id = 103, name = "abc3", classId = 2, Salary = 300 });  
         obj.Add(new student { id = 104, name = "abc4", classId = 2, Salary = 800 });  
         obj.Add(new student { id = 105, name = "abc5", classId = 2, Salary = 900 });  
         obj.Add(new student { id = 106, name = "abc6", classId = 3, Salary = 250 });  
         obj.Add(new student { id = 107, name = "abc7", classId = 3, Salary = 260 });  
         obj.Add(new student { id = 108, name = "abc8", classId = 3, Salary = 290 });  
         return obj;  
       }  
     }  
     static void Main(string[] args)  
     {  
       student s = new student();  
       var students = s.getStudents().ToList();  
       var firsthighest = students.OrderByDescending(x => x.Salary).Select(x => x.Salary).First();  
       var secondhighest = students.OrderByDescending(x => x.Salary).Where(x => x.Salary != firsthighest).Select(x => x.Salary).First();  
       var secondhighest1 = students.OrderByDescending(x => x.Salary).Skip(2).Select(x => x.Salary).First();  
       var secondhighest2 = students.Where(x => x.Salary != (students.Max(x1 => x1.Salary))).Select(x => x.Salary).Max();  
       Console.WriteLine(secondhighest1);  
       Console.WriteLine(secondhighest2);  
       Console.ReadLine();  
     }  
   }  
 }  


Output




Thursday 31 July 2014

To Find uncommon things between two List. And how interesting it is to know Concat Union and Distinct works

In the Last blog we saw how to find the uncommon things in particular list.
http://linqdetails.blogspot.in/2014/07/llinq-except.html


Now we need to find the uncommon things in both the list. We will continue with same example. First we will start with simple integer type , then we will go for class.


using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 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> num2NotinNum1 = num2.Where(x => !num1.Contains(x)).Distinct().ToList();// 4 12  
     List<int> num1NotinNum2 = num1.Where(x => !num2.Contains(x)).Distinct().ToList(); //9 13  
     List<int> CommoninNum1Num2 = num1.Intersect(num2).ToList(); // 1 7  
     List<int> uncommonBoth = num1.Union(num2).Except(CommoninNum1Num2).ToList();  
     foreach (int uncommon in uncommonBoth)  
     {  
       Console.WriteLine(uncommon);  
     }  
     Console.ReadLine();  
   }  
 }  










It works for

List<int> uncommonBoth = num1.Concat(num2).Except(CommoninNum1Num2).ToList();

So what is the difference between Concat and Union ???

I have explained here.

http://linqdetails.blogspot.com/2014/07/difference-between-concat-and-union.html

================================================================

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 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 = "Anurag", Id = "3" });  
     list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
     List<list1> list1NotInList2 = list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).ToList();  
     List<list2> list2NotInList1 = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName)).ToList();  
     Console.WriteLine("====Items in LIst1 but Not in list2===========");  
     foreach (list1 items in list1NotInList2)  
     {  
       Console.WriteLine(items.FirstName + " " + items.Id);  
     }  
     Console.WriteLine("====Items in LIst2 but Not in list1============");  
     foreach (list2 items in list2NotInList1)  
     {  
       Console.WriteLine(items.FirstName + " " + items.Id);  
     }  
   }  
 }  





           






-------------------Now we want both the things---------------------------------------------------------------


var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName)).ToList().Cast<object>().Union  
               (list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).ToList().Cast<object>());  
       foreach (dynamic items in finalcommonresult)  
       {  
         Console.WriteLine(items.FirstName + " " + items.Id);  
       }  

Biswa 1
Prasad 3
Abhi 1
Siba 3

-- This gives me both the resullts.
-- We can use Concat too
-- You need to use dynamic otherwise you will get error. items will be resolved at runtime whether its from list1 or list2. if from list1 then at runtime the firstname will be resolved as of list1.

----------------------------------------------------------------------------------------------------------

list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
       list1.Add(new list1 { FirstName = "Anurag", Id = "7" });  
       list1.Add(new list1 { FirstName = "Biswa", Id = "3" });  
       list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
       list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
       list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
       list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
             .Select(x=>x.FirstName).ToList().Cast<object>()  
             .Union(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName))  
             .Select(x => x.FirstName).ToList().Cast<object>());
       


Here we get
Abhi 
Siba
Biswa

The same thing with concat will give

 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
             .Select(x=>x.FirstName).ToList().Cast<object>()  
             .Concat(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).ToList()  
             .Select(x=>x.FirstName).Cast<object>()); 

Abhi
Siba
Biswa
Biswa

--------------------------------------------------------------------------------------------------------------------------


 list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
 list1.Add(new list1 { FirstName = "Anurag", Id = "7" });  
 list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
 list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
 list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .ToList().Cast<object>().Union(list1.Where(x => !list2  
 .Any(y => y.FirstName == x.FirstName)).ToList().Cast<object>());

            
Here we get

Abhi   1
Siba    3
Biswa 1
Biswa 1

If we are not selecting anything even if both same the records [Biswa 1 and Biswa 1] are same, union gives all the results.
-----------------------------------------------------------------------------------------------
So lets try selecting both the items. Earlier we have selected First Name. Now to select both items use anonymous type. Use the new keyword followed by curly braces.

 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .Select( x=> new { x.FirstName,x.Id}).ToList().Cast<object>()  
 .Union(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName))  
 .Select(x => new { x.FirstName, x.Id }).ToList().Cast<object>()); 

Abhi   1
Siba    3
Biswa 1

Cool :)

-------------------------------------------------------------------------------------------------------


 list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
 list1.Add(new list1 { FirstName = "Anurag", Id = "7" });  
 list1.Add(new list1 { FirstName = "Biswa", Id = "9" });  
 list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
 list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
 var finalcommonresult = list2  
 .Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .Select( x=> new { x.FirstName,x.Id}).ToList().Cast<object>()  
 .Union(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).Select(x => new { x.FirstName, x.Id }).ToList().Cast<object>());           


Abhi   1
Siba    3
Biswa 1
Biswa 9

ok cool :)

--------------------------------------------------------------------------------------------------------
You can also use Distinct for above queries.

Distinct also works when we are filtering on anonymous types.

var distinctlist1 = list1.Select(x=> new {x.FirstName,x.Id}).ToList().Distinct(); // this will give distinct  
  var distinctlist11 = list1.ToList().Distinct();//this will not give distinct 

So we can use Distinct + Concat. Because Distinct + Union will not make any sense as Union will take the distinct if we are working on anonymous types. 



var finalcommonresult =   
 list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .Select(x=> new {x.FirstName,x.Id}).ToList().Distinct().Cast<object>()  
 .Concat(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName))  
 .Select(x => new { x.FirstName, x.Id }).ToList().Distinct().Cast<object>());



To select a distinct you need to do as anonymous type too.

---------------------------------------------------------------------------------------------------------










Difference Between Concat and Union


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,13 };  
       List<int> concat = num1.Concat(num2).ToList();  
       List<int> union = num1.Union(num2).ToList();  
       Console.WriteLine("=======Concat Begins=======");  
       foreach (int concatnum in concat)  
       {  
         Console.WriteLine(concatnum);  
       }  
       Console.WriteLine("=======Union Begins=======");  
       foreach (int unionnum in union)  
       {  
         Console.WriteLine(unionnum);  
       }  
    }  
 }  

Output
















Union gives the distinct values. But Concat gives us all the things between both the list.
By this fact we can say that Concat() is faster than Union() because it doesn't do any processing. 

Wednesday 30 July 2014

Convert One List to Another List without For loop or Foreach Loop



  • Convert One List to Another List

Code
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 namespace OverloadingConstructor  
 {  
   class TargetList  
   {  
     public TargetList(string Id,string FirstName,String LastName)  
     {  
       this.LastName = LastName;  
       this.FirstName = FirstName;  
       this.Id = Id;  
     }  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
   }  
   class SourceList  
   {  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
   }  
   class Program  
   {  
     public static TargetList PointFToPoint(SourceList pf)  
     {  
       return new TargetList(pf.Id,pf.FirstName,pf.LastName);  
     }  
     static void Main(string[] args)  
     {  
       List<SourceList> sourceList = new List<SourceList>();  
       sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1" });  
       sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2" });  
       sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3" });  
       sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4" });  
       List<TargetList> Target = sourceList.ConvertAll(new Converter<SourceList, TargetList>(PointFToPoint));  
       foreach (var item in Target)  
       {  
         Console.WriteLine("First Name::- " + item.FirstName + "; Last Name::- " +item.LastName + "; Id:- " + item.Id);  
       }  
       Console.ReadLine();  
     }  
   }  
 }  

Debug



Output









Second Way

Code
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 namespace OverloadingConstructor  
 {  
   class TargetList  
   {  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
     public List<string> DepId { get; set; }  
   }  
   class SourceList  
   {  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
     public List<string> DepId { get; set; }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       List<SourceList> sourceList = new List<SourceList>();  
       sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1", DepId = new List<string> { "1","2","3"} });  
       sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2", DepId = new List<string> { "11", "21", "31" } });  
       sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3", DepId = new List<string> { "21", "22", "23" } });  
       sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4", DepId = new List<string> { "31", "32", "33" } });  
       List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList { FirstName=x.FirstName,LastName=x.LastName, Id=x.Id, DepId=x.DepId}).ToList();  
       foreach (var item in Target1)  
       {  
         Console.WriteLine("First Name::- " + item.FirstName + "; Last Name::- " + item.LastName + "; Id:- " + item.Id);  
       }   
       Console.ReadLine();  
     }  
   }  
 }  

Debug


Llinq Except and How it Differs from Not In Operator

Contains & Except


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  :)