1

May be my problem is common. I have seen it here Comparing Two collections. But I want to compare two complex collections.

public class Project
{
    public Int32 ProjectID { get; set; }
    public String ProjectName { get; set; }
    public String ProjectCode { get; set; }
}
public class Unit
{
    public Int32 UnitID { get; set; }
    public Int32 ProjectID { get; set; }
    public String UnitName { get; set; }
    public String UnitCode { get; set; }
}

Here Project and Unit have a Common field called ProjectID.And I have a Collection of Project and Unit. Corresponding to each project in the project collection, there is a unit in the unit collection. And some of the units in the unit collection is not belonging to any project in the project collection. I want to Filter out those units. How can I do that using Linq.

Community
  • 1
  • 1
amesh
  • 1,311
  • 3
  • 21
  • 51
  • The following references will give you fairly good idea, how to compare complex collections: 1. [Custom Generic Compare For Collection][1] 2. http://stackoverflow.com/questions/50098/comparing-two-collections-for-equality [1]:http://www.codeproject.com/Articles/16829/Custom-Generic-Compare-For-Collection – Furqan Safdar Oct 13 '12 at 06:58

2 Answers2

2

You can use LINQ:

var result = units.Where(u => projects.All(p => p.ProjectID != u.ProjectID));
cuongle
  • 74,024
  • 28
  • 151
  • 206
1
from u in unitCollection
from p in projectCollection
Where p.ProjectID == u.ProjectID
select u 
Typist
  • 1,464
  • 9
  • 14