Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq比较两个不同的列表并选择外部联接_C#_Linq - Fatal编程技术网

C# Linq比较两个不同的列表并选择外部联接

C# Linq比较两个不同的列表并选择外部联接,c#,linq,C#,Linq,我有两个不同的类,它们表示两种类型的数据。第一种是未上市的原始格式。第二个是发布格式 public class SalesRecords { public long? RecordId { get; set; } public DateTime RecordDate { get; set; } public string RecordDesc { get; set; } // Other non-related properties and methods

我有两个不同的类,它们表示两种类型的数据。第一种是未上市的原始格式。第二个是发布格式

 public class SalesRecords
 {
    public long? RecordId { get; set; }
    public DateTime RecordDate { get; set; }
    public string RecordDesc { get; set; }

    // Other non-related properties and methods
 }

 public class PostedSalesRecords
 {
      public long? CorrelationId { get; set; }
      public DateTime RecordDate { get; set; }
      public DateTime? PostedDate { get; set; }
      public string RecordDesc { get; set; }

      // Other non-related properties and methods
 }
我们的系统有一个销售记录列表。这些销售记录在用户确定的时间发布到不同的系统。我正在创建一个屏幕,显示所有已过账的销售记录以及未过账的销售记录作为对账。my grid的数据源将是PostedSalesRecords的列表。我需要做的是找出
列表
中哪些记录不在
列表
中,然后将那些未列出的SalesRecords映射到PostedSalesRecords。我很难找到快速比较的方法。基本上我试过了,速度非常慢:

 private List<SalesRecords> GetUnpostedSalesRecords(
      List<SalesRecords> allSalesRecords,
      List<PostedSalesRecords> postedSalesRecords)
 {
      return allSalesRecords.Where(x => !(postedSalesRecords.Select(y => y.CorrelationId.Value).Contains(x.RecordId.Value))).ToList();
 }
私有列表GetUnpostedSalesRecords(
列出所有销售记录,
列出postedSalesRecords)
{
返回allSalesRecords.Where(x=>!(postedSalesRecords.Select(y=>y.CorrelationId.Value)。Contains(x.RecordId.Value)).ToList();
}

我最大的问题是我在过滤大量数据。我将55000条总销售记录与17000条已发布记录进行比较。我花了大约2分钟来处理这个。有没有可能加快速度?谢谢

您可以尝试外部联接,请查看这是否有助于提高性能:

 var test = (from s in allSalesRecords
                join p in postedSalesRecords on s.RecordId equals p.CorrelationId into joined
                from j in joined.DefaultIfEmpty()
                where j == null
                select s).ToList();
或者在您的实现中,您可以为postedSalesRecords创建一个只包含ID的字典,然后在查询中使用该集合,这肯定会有助于提高性能,因为查找时间将为O(1),而不是遍历每个记录的整个集合

 var postedIds = postedSalesRecords.Select(y => y.CorrelationId.Value)
                                      .Distinct().ToDictionary(d=>d);
return allSalesRecords.Where(x => !(postedIds.ContainsKey(x.RecordId.Value))).ToList();
使用左外部联接应能更有效地工作:

private List<SalesRecords> GetUnpostedSalesRecords(
    List<SalesRecords> allSalesRecords,
    List<PostedSalesRecords> postedSalesRecords)
{
    return (from x in allSalesRecords
            join y in postedSalesRecords on x.RecordId.Value
                                     equals y.CorrelationId.Value into joined
            from z in joined.DefaultIfEmpty()
            where z == null
            select x).ToList();
}
私有列表GetUnpostedSalesRecords(
列出所有销售记录,
列出postedSalesRecords)
{
返回(来自allSalesRecords中的x)
在x.RecordId.Value上的postedSalesRecords中加入y
等于y.CorrelationId.Value到已联接的
从joined.DefaultIfEmpty()中的z开始
其中z==null
选择x).ToList();
}

这可能会通过哈希集实现。您可以自己实现这一点(可以这样说更清楚):在一个或两个列表中构建一个ID值的
HashSet
,以确保每次查看外部列表时不需要重复的O(N)查找。

。几乎是瞬间的。非常感谢。