none
How to compare two different structure datatable by using LINQ RRS feed

  • Question

  • Hi,

        i have following senario in my application

    DataTable1 have following structure 

    Dependencies                              Tolerance

    Workflow1 Lorem1 lpsum                      150  
    Workflow2 Lorem1 lpsum                       25
     
    DataTable 2

    Dep_Type         Check      Dependencies

    "Star Workflow" "false" "Workflow1 Lorem1 lpsum"
    "Star Workflow" "false" "Workflow2 Lorem1 lpsum"
    Star Workflow" "false" "Workflow4 Lorem lpsum"
    Star Workflow" "false" "Workflow5 Lorem lpsum" 

    I want to compare two datatable  by using Dependencies.  

    My Output should come like that, how i can achieve it through LINQ

    Star Workflow" "false" "Workflow4 Lorem lpsum"
    Star Workflow" "false" "Workflow5 Lorem lpsum" 

     

    Please any one help me

    Friday, October 22, 2010 11:12 AM

Answers

  • Hi,

    This code is returning you required result in LinqPad:

    public class DataTable1
    {
     public string Dependencies { get; set; }
     public int Tolerance { get; set; }
    }
    
    public class DataTable2
    {
     public string Dep_Type { get; set; }
     public bool Check { get; set; }
     public string Dependencies { get; set; }
    }
    
    void Main()
    {
    
    List<DataTable1> List1 = new List<DataTable1>{
     new DataTable1 { Dependencies = "Workflow1 Lorem1 lpsum", Tolerance = 150 },
     new DataTable1 { Dependencies = "Workflow2 Lorem1 lpsum", Tolerance = 25 }
    };
    
    List<DataTable2> List2 = new List<DataTable2>{
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow1 Lorem1 lpsum" },
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow2 Lorem1 lpsum" },
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow4 Lorem1 lpsum" },
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow5 Lorem1 lpsum" } 
    };
    var list = 
    	(from d2 in List2 
    	 where !(from d1 in List1 
    		 	 select d1.Dependencies)
    		  .Contains(d2.Dependencies) 
    	 select new {Dev_Type = d2.Dep_Type,
    				 Check = d2.Check,
    				 Dependencies = d2.Dependencies }).ToList();
    
    
    foreach (var item in list)
     Console.WriteLine(item.Dev_Type + " " + item.Check + " " + item.Dependencies);
    
    }
    
    

    Best regards,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by liurong luo Wednesday, October 27, 2010 9:05 AM
    Friday, October 22, 2010 2:19 PM

All replies

  • Hi,

    This code is returning you required result in LinqPad:

    public class DataTable1
    {
     public string Dependencies { get; set; }
     public int Tolerance { get; set; }
    }
    
    public class DataTable2
    {
     public string Dep_Type { get; set; }
     public bool Check { get; set; }
     public string Dependencies { get; set; }
    }
    
    void Main()
    {
    
    List<DataTable1> List1 = new List<DataTable1>{
     new DataTable1 { Dependencies = "Workflow1 Lorem1 lpsum", Tolerance = 150 },
     new DataTable1 { Dependencies = "Workflow2 Lorem1 lpsum", Tolerance = 25 }
    };
    
    List<DataTable2> List2 = new List<DataTable2>{
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow1 Lorem1 lpsum" },
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow2 Lorem1 lpsum" },
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow4 Lorem1 lpsum" },
     new DataTable2 { Dep_Type = "Star Workflow", Check = false, Dependencies = "Workflow5 Lorem1 lpsum" } 
    };
    var list = 
    	(from d2 in List2 
    	 where !(from d1 in List1 
    		 	 select d1.Dependencies)
    		  .Contains(d2.Dependencies) 
    	 select new {Dev_Type = d2.Dep_Type,
    				 Check = d2.Check,
    				 Dependencies = d2.Dependencies }).ToList();
    
    
    foreach (var item in list)
     Console.WriteLine(item.Dev_Type + " " + item.Check + " " + item.Dependencies);
    
    }
    
    

    Best regards,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by liurong luo Wednesday, October 27, 2010 9:05 AM
    Friday, October 22, 2010 2:19 PM
  • JA Reyes , You are savior ...! I found ur solution after whole day and it works......!! :)
    Monday, March 14, 2011 8:45 AM
  • Thank you ShahJaynesh,

    You can "Vote As Helpful" to allow other users find better this solution if they have the same problem too.

    Cheers.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    Monday, March 14, 2011 8:55 AM