0

I am getting this error while using this IEquality comparer for intersection. Can somebody identify where I am doing it wrong?

Pleas ask if you need more information.

  • 1
    Help us help you - please share your code and the exact error message. – Mureinik Feb 10 '19 at 07:59
  • 1
    Why are you using so many `join`s? If you're using Entity Framework (aka Linq-to-Entities) then you should have Navigation Properties and `IQueryable.Include( e => e.Property )` to avoid all of those. – Dai Feb 10 '19 at 08:03
  • 1
    Also, I recommend rewriting your query entirely using Extension Methods and avoiding the `from o in join x in y` syntax to be more consistent and to make code-formatting much easier to read. – Dai Feb 10 '19 at 08:03
  • @Mureinik Added error message and some more code – user7739249 Feb 10 '19 at 08:41
  • remove the `.ToList()` from `ComparableArticle {ArticleId = a.ArticleId}).ToList()` in all places. You need to convert to ToList() after the query is executed. [More info](https://stackoverflow.com/questions/10333619/how-to-convert-linq-query-result-to-list) – karthickj25 Feb 10 '19 at 09:02
  • Then how will i compare the count,i will get the error here oa.Intersect(da, new ArticleComparer()).Count().Equals(oa.Count) – user7739249 Feb 10 '19 at 09:16

1 Answers1

3

When you write LINQ to Entities, you must remember that eventually all of your LINQ expression is translated to SQL. So for every method call you make in that expression, you should think if there's a reasonable way to translate it to SQL.

Having that in mind, you can see that:

  1. SQL has no notion of .ToList(), which is probably the source of your current error. calls to .ToList() should be made at the end of the expression, as a way to "materialize" the query (i.e. make EF make the actual call to the database and return results).
  2. Your database knows nothing about C# interfaces. You can't expect any implementation of IEqualityComparer to be translatable to SQL.
  3. As @Dai has noted, you seem to be using too many joins. Make sure your model has the right navigation properties between entities, and use them.
Tsahi Asher
  • 1,767
  • 15
  • 28