tags:

views:

45

answers:

1

Hi i have a List<my_Custom_Class> and List<string>. My custom class has various items in which one is DCN number and list<string> contains only DCN number. So I need to check the List<Custom_Class> contains any dcn from List<string>.

For example suppose List1 = List<Custom_Class> and List2 = List<String>. If List1 has 2000 items and list2 has 40000 items on which 600 items from List1 exists in List2. So in this case i need 1400 as my output List as list1. So what would be the expression. Also one more case is here since List1 contains various items , other items values might be different but the DCN must be same. That I need to check only the similarity of DCN.

Below is my class. Now from datatable i am adding DCN to a List<String> Dcns;

 public class DocumentInfo
    {

        public string ImageType { get; set; }
        public string FileFullPath { get; set; }
        public string BatchName { get; set; }
        public string FileName { get; set; }
        public string DCN { get; set; }
        public string MemberID { get; set; }
        public string NPI { get; set; }
        public string TaxID { get; set; }
        public string Client { get { return DCN.Substring(0, 2); } }
     }

So here in my case I have 493K DCNS in List<string>Dcns and 110K DCNS in List<DocumentInfo>. I need to filter the duplicates records from 110K DCNs. Or can say substract the two list. I need to remove all duplicate dcns that exists in List. Say If 70K records are duplicate then my resultant list of document info will be of List of rest 40K. Distinct/Except is not working. LINQ/ Lambda Exp will be preferrable.

I need it urgently and ASAP.

Thanks in adv.

+4  A: 
var dcnSet = new HashSet<string>(Dcns);
docInfoList.RemoveAll(el => dcnSet.Contains(el.DCN));

The HashSet isn't strictly necessary, but it makes the difference between O(m * n) and O(m + n), where is m and n are the number of elements in each list.

EDIT:

RemoveAll removes the elements from the existing List in place. It does not create a new List. It returns the number of elements removed, but if you don't care about that you can just discard the return value.

Matthew Flaschen
what is el here?
Amit Ranjan
Its returning me int and i need the list.. please help
Amit Ranjan
`el` is a parameter for the lambda expression, holding the current element. That lambda is a predicate (boolean function) that will be called for each element of `docInfoList`. It is returns true, the element will be removed.
Matthew Flaschen
Thanks , alot, it worked ... you are a life saver Mathew Flaschen..:)
Amit Ranjan