List of filters from the list using Linq


I have retrieved list of my specific class with 150 records. Now, i want only those records which have Licenseid which are in my another int List.

For example My MainList

List<CustomerDocument> _list = GetListByID(CustomerID);

In this list i have column LicenseID,CustomerID,CustomerName,Type,Age e.t.c

And SecontList

List<int> type = new List<int>();

In Int list i add LicenseID one by one dynamically.

Public class CustomerDocument
 public int LicenseID{get;set;};
 public int CustomerID{get;set;};
 public string CustomerName{get;set;};
 public int Age{get;set;};

This is my CustomerDocument class for which i am getting list.

And now suppose, If Int list has three records , then i want those records from my Main List which have these three LicenseID in my Int List using Linq.

_list = ??? 

 List<CustomerDocument> list = new List<CustomerDocument>();
 List<Int> types = new List<Int>();
 MapSearchSalesEntities datacontext = new MapSearchSalesEntities();

 var collection = ddlLicenseType.CheckedItems;
 if (collection.Count > 0)
            foreach (var item in collection)
                int value = Convert.ToInt32(item.Value);
                types .Add(value);

var query = (from t1 in datacontext.Licenses
              select new CustomerDocument
                       LicenseID = t1.LicenseID,
                       CustomerID = t1.CustomerID,
                       CustomerName= t1.CustomerName,
                       Age= t1.Age,
list = query.ToList(); ---gives 150 Records

  if (types != null && types.Count > 0)
   list = list.Where(c => types.Contains(c.LicenseID)).ToList(); --- Gives 0 Records

The most efficient approach is to use Enumerable.Join:

var documents = from doc in _list
                join licenseID in type
                on doc.LicenseID equals licenseID
                select doc;

if you want to replace the list:

_list = documents.ToList();

You could also use Enumerable.Where + List.Contains which is not as efficient but shorter:

_list = _list.Where(d => type.Contains(d.LicenseID)).ToList();