C# 如何比较LINQ结果?

C# 如何比较LINQ结果?,c#,linq,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 &&

我试图找到一种方法来比较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 };
有什么建议吗

//更新为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 };
实际上,您正在尝试比较在后台生成的两个不同的匿名类(
System.Linq.ParallelQuery或)

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

匿名类不是这样,因为编译器将为它们生成这些方法

要了解更多信息: ,

创建一个简单类(例如命名为result)并生成该类型的查询结果。 然后
重写类的
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
}
公共类结果{
公共字符串a{get;set}
公共字符串b{get;set}
公共字符串c{get;set}
公共字符串d{get;set}
//这是equals实现的一个简短的不完整版本
//咨询其他问题以了解更多关于平等的信息
公共覆盖布尔等于(结果其他)
{返回other.a==a&&other.b==b&&other.c==c&&other.d==d}
}
//您必须向查询中添加另一个ORDERBY子句,以便它们具有相同的顺序
变量集=
(来自一名住院患者。AsParallel()
来自患者b.AsParallel()
来自患者c.AsParallel()
来自患者d.AsParallel()
其中a.VisitNum

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

实际上,您正在尝试比较在后台生成的两个不同的匿名类(
System.Linq.ParallelQuery或)

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

匿名类不是这样,因为编译器将为它们生成这些方法

要了解更多信息: ,

创建一个简单类(例如命名为result)并生成该类型的查询结果。 然后
重写类的
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
}
公共类结果{
公共字符串a{get;set}
公共字符串b{get;set}
公共字符串c{get;set}
公共字符串d{get;set}
//这是equals实现的一个简短的不完整版本
//咨询其他问题以了解更多关于平等的信息
公共覆盖布尔等于(结果其他)
{返回other.a==a&&other.b==b&&other.c==c&&other.d==d}
}
//您必须向查询中添加另一个ORDERBY子句,以便它们具有相同的顺序
变量集=
(来自一名住院患者。AsParallel()
来自患者b.AsParallel()
来自患者c.AsParallel()
来自患者d.AsParallel()
其中a.VisitNum



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

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

在我尝试了许多解决方案之后,我在彼此内部制作了两个foreach来比较元素在集合内部,但这种解决方案非常糟糕,需要很长时间来比较一个巨大的数据,我猜它们都是空的。离题,但仅供参考-这是获取
a
所有组合的一种非常糟糕的方法。这是非常低效的。你应该排序一次,然后你可以对
i,j,k,l
进行排序i
,匹配的元素是一个组合。@Some1.Kill.the.DJ你说得对,集合是空的你真的没有检查它们的内容。@Yorye Nathan我不明白你的意思,你能解释更多吗?我猜它们都是空的。。脱离主题,但仅供参考-这是获取
a
。这是非常低效的。你应该排序一次,然后你可以说,对于索引
i,j,k,l
,即
i
,匹配的元素是一个组合。@Some1.Kill.the.DJ你是对的,集合是空的。你真的没有检查它们的内容。@Yorye Nathan我不明白你的意思,你能解释更多吗?我已按照您的建议进行了操作,但我遇到了以下错误4方法“System.Linq.Enumerable.SequenceEqual”的类型参数(System.Collections.Generic.IEnumerable,System.Collections.Generic.IEnumerable)'无法从用法中推断。请尝试显式指定类型参数。我更正了错误,但现在每次都给我false,我都不知道为什么我确信它们必须具有相同的顺序。此代码列表中的类型是什么patient=new List();LI
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
}