简体   繁体   中英

Finding the difference between the list

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM