1

我正在尝试找到一种方法来比较 LINQ 结果中的元素

这是 LINQ 代码

var sets =
         from a in patient.AsParallel()
         from b in patient.AsParallel()
         from c in patient.AsParallel()
         from d in patient.AsParallel()



where a.VisitNum < b.VisitNum && b.VisitNum < c.VisitNum && c.VisitNum < d.VisitNum
select new { a, b, c, d };

var sets1 =
                 from a in patient1.AsParallel()
                 from b in patient1.AsParallel()
                 from c in patient1.AsParallel()
                 from d in patient1.AsParallel()
select new { a, b, c, d };

我用它来比较这段代码,但每次都给我假

 if (Enumerable.SequenceEqual(sets, sets1) == true)

有什么建议吗?

// 更新为 Jani 的答案

public class Result
    {
        public ACVsize5 a { get; set; }
        public ACVsize5 b { get; set; }
        public ACVsize5 c { get; set; }
        public ACVsize5 d { get; set; }


}

public override Boolean Equals(Result other)
        {
            return other.a.date.ToString() == a.date.ToString() && other.a.RaId.ToString() == a.RaId.ToString() && other.b.date.ToString() == b.date.ToString() && other.b.RaId.ToString() == b.RaId.ToString() && other.c.date.ToString() == c.date.ToString() && other.c.RaId.ToString() == c.RaId.ToString() && other.d.date.ToString() == d.date.ToString() && other.d.RaId.ToString() == d.RaId.ToString();
        }

var sets =
             from a in patient
             from b in patient
             from c in patient
             from d in patient
where a.VisitNum < b.VisitNum && b.VisitNum < c.VisitNum && c.VisitNum < d.VisitNum              

select new Result { a = a, b = b, c = c, d = d };
var sets1 =
             from t in patient1
             from y in patient1
             from u in patient1
             from p in patient1
where t.VisitNum < y.VisitNum && y.VisitNum < u.VisitNum && u.VisitNum < p.VisitNum 
             select new Result { a = t, b = y, c = u, d = p };

但我在覆盖方法上遇到错误

//Error 1 no suitable method found to override
4

2 回答 2

1

实际上,您正在尝试比较已在后台生成的两个不同的匿名类(System.Linq.ParallelQuery<AnonymousType#1>''System.Linq.IQueryable<AnonymousType#2>')。

我错了,正如@Allon 所说,只会为相同的结构生成一个类。

每当您在 Linq 查询中使用 select new 而不在左括号前指定类名时,将在后台生成一个匿名类,您可以通过ILDasmReflector等工具看到它。

另一个重要的一点是,当您比较您声明的类型的对象(不是 .NET Framework 库的一部分)时,它们将通过它们的引用而不是它们的内容进行比较。因此,您必须通过重写 Equals 方法来定义自己的相等实现。

对于匿名类,情况并非如此,因为编译器将为它们生成这些方法。

要了解更多信息: 1 , 2

创建一个简单的类(例如命名结果)并生成该类型的查询结果。然后类overrideEquals方法并使用SequenceEqual. 一切都会好的。

public Class Result{
  public string a {get;set}
  public string b {get;set}
  public string c {get;set}
  public string d {get;set}
  //this is a short incomplete version of equals implementation
  //consult other questions to learn more about equality
  public override boolean Equals(Result other)
  { return other.a == a && other.b == b && other.c == c && other.d == d}
}

//you must add another order by clause to query so that both of them have the same order
var sets =
     (from a in patient.AsParallel()
     from b in patient.AsParallel()
     from c in patient.AsParallel()
     from d in patient.AsParallel()
     where a.VisitNum < b.VisitNum && b.VisitNum < c.VisitNum && c.VisitNum < d.VisitNum
     select new Result{ a = a, b = b, c = c, d = d }).AsEnumerable();
var sets1 =
     (from a in patient1.AsParallel()
     from b in patient1.AsParallel()
     from c in patient1.AsParallel()
     from d in patient1.AsParallel()
     select new Result{ a = a, b = b, c = c, d = d }).AsEnumerable();
//Now it would be right
if (Enumerable.SequenceEqual(sets, sets1))
{
   //do your stuff
}

您只需要确保这些序列具有相同的顺序。

于 2012-12-02T14:41:30.727 回答
0

在我尝试了很多解决方案之后,我在彼此内部做了两个 foreach 来比较集合内的元素,但是这个解决方案非常糟糕,需要很长时间来比较大量数据

于 2012-12-03T19:17:30.503 回答