Wimaxmax
Wimaxmax

Reputation: 95

How I can compare the LINQ result?

I am trying to find a method to compare the element inside LINQ result

This is the LINQ code

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

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

Upvotes: 1

Views: 290

Answers (2)

Wimaxmax
Wimaxmax

Reputation: 95

After I tried many soluton I made two foreach inside each other to compare the element inside the sets but this solutotion is very bad and take long time to compare a huge data

Upvotes: 0

Jahan Zinedine
Jahan Zinedine

Reputation: 14864

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>').

I was wrong and as stated by @Allon, there will be generated just one class for the same structure.

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.

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. Thus you must define your own implementation of equality, by overriding the Equals method.

That's not the case for anonymous classes cause the compiler will generate those methods for them.

To learn more: 1, 2

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. 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.

Upvotes: 1

Related Questions