简体   繁体   English

我如何比较LINQ结果?

[英]How I can compare the LINQ result?

I am trying to find a method to compare the element inside LINQ result 我试图找到一种方法来比较LINQ结果中的元素

This is the LINQ code 这是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 };

and this code I used it to compare but every time give me false 和我用来比较的这段代码,但每次都给我错误

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

Any suggestion ? 有什么建议吗?

// update as Jani's Answer //更新为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 };

but I got error on override methods 但是我在覆盖方法上出错

//Error 1 no suitable method found to override

Actully you are trying to compare two different anonymous classes which has been generated under the hood ( System.Linq.ParallelQuery<AnonymousType#1>' and 'System.Linq.IQueryable<AnonymousType#2>' ). 实际上,您正在尝试比较已在后台生成的两个不同的匿名类( System.Linq.ParallelQuery<AnonymousType#1>''System.Linq.IQueryable<AnonymousType#2>' )。

I was wrong and as stated by @Allon, there will be generated just one class for the same structure. 我错了,正如@Allon所说,对于相同的结构只会生成一个类。

Whenever you use the select new in a Linq query without specifying the class name before open bracket, an anonymous class will be generated under the hood which you can see it by tools like ILDasm or Reflector . 每当您在Linq查询中使用select new而不在尖括号前指定类名时,都会在内部生成一个匿名类,您可以使用ILDasmReflector之类的工具查看该类。

Another important point is that when you compare objects of a type which you declared(not part of .NET Framework Library), they will be compared by their references not by their content. 另一个重要的一点是,当您比较声明的类型的对象(不是.NET Framework库的一部分)时,将通过引用而不是内容对其进行比较。 Thus you must define your own implementation of equality, by overriding the Equals method. 因此,您必须通过覆盖Equals方法来定义自己的平等实现。

That's not the case for anonymous classes cause the compiler will generate those methods for them. 匿名类不是这种情况,因为编译器将为其生成这些方法。

To learn more: 1 , 2 要了解更多信息: 12

Create a simple class (named result for example) and make the result of query of that type. 创建一个简单的类(例如,命名结果)并进行该类型查询的结果。 Then override the Equals method of the class and use the SequenceEqual . 然后 override该类的 Equals方法,并使用 SequenceEqual everything will be right. 一切都会好起来的。

 
 
 
  
  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 }
 
  

You just need to ensure that those sequences have the same order. 您只需要确保这些序列具有相同的顺序即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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