我如何比较LINQ结果?

时间:2012-12-02 13:54:15

标签: c# linq

我正在尝试找到一种方法来比较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

2 个答案:

答案 0 :(得分:1)

实际上,您正在尝试比较两个不同的匿名类,这些类是在引擎盖下生成的(System.Linq.ParallelQuery<AnonymousType#1>''System.Linq.IQueryable<AnonymousType#2>')。

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

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

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

匿名类不是这种情况,因为编译器会为它们生成这些方法。

要了解更多信息: 12

创建一个简单的类(例如命名结果)并生成该类型的查询结果。 然后override该类的Equals方法并使用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
}

<击>

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

答案 1 :(得分:0)

在我尝试了许多解决方案之后,我在彼此内部制作了两个foreach以比较集合中的元素,但这种解决方案非常糟糕并且花费很长时间来比较大量数据