代码之家  ›  专栏  ›  技术社区  ›  user240141

查找列表之间的差异

  •  1
  • user240141  · 技术社区  · 14 年前

    我有一个 List<my_Custom_Class> and List<string> . 我的自定义类有许多项,其中一项是DCN number和 list<string> 仅包含DCN编号。所以我需要检查一下 List<Custom_Class> contains any dcn from List<string>.

    例如,假设 List1 = List<Custom_Class> and List2 = List<String> 这里还有一个例子,因为List1包含不同的项,其他项的值可能不同,但DCN必须相同。我只需要检查DCN的相似性。

    下面是我的班级。现在我从datatable将DCN添加到 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); } }
         }
    

    所以在我的案例中,我有493K个DCN List<string>Dcns and 110K DCNS in List<DocumentInfo>

    我需要它紧急和尽快。

    先谢谢你。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Matthew Flaschen    14 年前
    var dcnSet = new HashSet<string>(Dcns);
    docInfoList.RemoveAll(el => dcnSet.Contains(el.DCN));
    

    HashSet 这并不是严格必要的,但是它使O(m*n)和O(m+n)有所不同,其中m和n是每个列表中元素的数量。

    编辑:

    RemoveAll 从现有图元中删除图元 List 列表 . 它返回删除的元素数,但如果您不在乎,可以放弃返回值。