See MSDN docs :
how to do inner join,left outerjoin between collections in C#
Hi Friends,
I have two collection with more number of items like a million. I want to compare them to find out the below in C#
//how to find the matching items between Orders1 and Orders2
//how to find the Orders1 which are in Orders2
//how to find the Orders2 which are in Orders1
//how to find the Orders1 which are not in Orders2
//how to find the Orders2 which are not in Orders1
public class Order
{
public int OrderID { get; set; }
public string OrderDate { get; set; }
public string StoreID { get; set; }
public float TotalPrice { get; set; }
}
static void Main(string[] args)
{
List<Order> Orders1 = new List<Order>();
Orders1.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders1.Add(new Order { OrderID = 2, StoreID = "A02", TotalPrice = 12.2F });
Orders1.Add(new Order { OrderID = 3, StoreID = "A03", TotalPrice = 12.3F });
Orders1.Add(new Order { OrderID = 4, StoreID = "A04", TotalPrice = 12.4F });
Orders1.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders1.Add(new Order { OrderID = 5, StoreID = "A05", TotalPrice = 12.5F });
List<Order> Orders2 = new List<Order>();
Orders2.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders2.Add(new Order { OrderID = 2, StoreID = "A02", TotalPrice = 12.2F });
Orders2.Add(new Order { OrderID = 2, StoreID = "A03", TotalPrice = 12.2F });
Orders2.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders2.Add(new Order { OrderID = 5, StoreID = "A05", TotalPrice = 12.5F });
Orders2.Add(new Order { OrderID = 6, StoreID = "A06", TotalPrice = 12.6F });
Orders2.Add(new Order { OrderID = 7, StoreID = "A07", TotalPrice = 12.7F });
//how to find the matching items between Orders1 and Orders2
//how to find the Orders1 which are in Orders2
//how to find the Orders2 which are in Orders1
//how to find the Orders1 which are not in Orders2
//how to find the Orders2 which are not in Orders1
}
2 answers
Sort by: Most helpful
-
-
Jack J Jun 24,496 Reputation points Microsoft Vendor
2022-10-18T09:46:00.077+00:00 @Code Heaven , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.
I think the three previous questions are actually one question, and they all return common items to both Orders1 and Orders2.
Also, we need to Implement the interface
IEqualityComparer
to useExcept
andIntersect
method to compare list of objects.public class OrderComparer : IEqualityComparer<Order> { public bool Equals(Order x, Order y) { if(x.OrderID==y.OrderID&&x.OrderDate==y.OrderDate&&x.TotalPrice==y.TotalPrice&&x.StoreID==y.StoreID) { return true; } else { return false; } } public int GetHashCode(Order obj) { return 1; } }
How to use it:
OrderComparer comparer = new OrderComparer(); var result = Orders1.Intersect(Orders2, comparer); //->how to find the matching items between Orders1 and Orders2 how to find the Orders1 which are in Orders2 how to find the Orders2 which are in Orders1 var d1 = Orders1.Except(Orders2, comparer); //how to find the Orders1 which are not in Orders2 var d2 = Orders2.Except(Orders1,comparer); //how to find the Orders2 which are not in Orders1
Hope my code could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.