سؤال

I am having trouble with this error message as the line it errors on is not actually using Distinct() at all. I am wondering if it is something to do with the IEqualityComparer I've implemented?

It creates an unsupported exception in a view on this line, specifically on .Count():

tr style="display: @( Model.FeaturedOffers.Count() == 0 ? "" : "none" ) " id="none">

Here is the IEqualityComparer class:

public class RewardOfferEqualityComparer : IEqualityComparer<OfferOverviewViewModel>
{
    public bool Equals(OfferOverviewViewModel x, OfferOverviewViewModel y)
    {
        return Equals(x.OfferId, y.OfferId);
    }

    public int GetHashCode(OfferOverviewViewModel x)
    {
        return x.OfferId.GetHashCode();
    }
}
هل كانت مفيدة؟

المحلول

Found that calling .ToList().Distinct() instead of .Distinct() solved the problem.

Found answer here

نصائح أخرى

You might not be using Distinct(), but somehere in the chain of enumerables represented by Model.FeaturedOffers I'm guessing there's an IQueryable expression that's using a Distinct call that is not supported by the underlying provider - for example Linq to SQL or Entity Framework.

For example - if you are querying a Table in Entity Framework and passing your own comparer then that most certainly won't work - since there's no way for EF to re-implement the comparer in SQL or eSQL

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top