views:

16

answers:

1

Hi,

I am using Entity Framework 4.0 and trying to use the "Contains" function of one the object sets in my context object. to do so i coded a Comparer class:

    public class RatingInfoComparer : IEqualityComparer<RatingInfo>
    {           
        public bool Equals(RatingInfo x, RatingInfo y)
        {
            var a = new {x.PlugInID,x.RatingInfoUserIP};
            var b = new {y.PlugInID,y.RatingInfoUserIP};

            if(a.PlugInID == b.PlugInID && a.RatingInfoUserIP.Equals(b.RatingInfoUserIP))
                return true;
            else
                return false;
        }

        public int GetHashCode(RatingInfo obj)
        {
            var a = new { obj.PlugInID, obj.RatingInfoUserIP };

            if (Object.ReferenceEquals(obj, null))
                return 0;
            return a.GetHashCode(); 
        }


    }

when i try to use the comparer with this code:

    public void SaveRatingInfo2(int plugInId, string userInfo)
    {

        RatingInfo ri = new RatingInfo()
        {
            PlugInID = plugInId,
            RatingInfoUser = userInfo,
            RatingInfoUserIP = "192.168.1.100"
        };

        //This is where i get the execption
        if (!context.RatingInfoes.Contains<RatingInfo>(ri, new RatingInfoComparer()))
        {
            //my Entity Framework context object
            context.RatingInfoes.AddObject(ri);
            context.SaveChanges();
        }
    }

i get an execption:

"LINQ to Entities does not recognize the method 'Boolean Contains[RatingInfo](System.Linq.IQueryable1[OlafCMSLibrary.Models.RatingInfo], OlafCMSLibrary.Models.RatingInfo, System.Collections.Generic.IEqualityComparer1[OlafCMSLibrary.Models.RatingInfo])' method, and his method cannot be translated into a store expression."

Since i am not proficient with linQ and Entity Framework i might be making a mistake with my use of the "var" either in the "GetHashCode" function or in general.

If my mistake is clear to you do tell me :) it does not stop my project! but it is essential for me to understand why a simple comparer doesnt work.

Thanks Aaron

A: 

LINQ to Entities works by converting an expression tree into queries against an object model through the IQueryable interface. This means than you can only put things into the expression tree which LINQ to Entities understands.

It doesn't understand the Contains method you are using, so it throws the exception you see. Here is a list of methods which it understands.

Under the Set Methods section header, it lists Contains using an item as supported, but it lists Contains with an IEqualityComparer as not supported. This is presumably because it would have to be able to work out how to convert your IEqualityComparer into a query against the object model, which would be difficult. You might be able to do what you want using multiple Where clauses, see which ones are supported further up the document.

Douglas
Thanks!!! The link about "Supported and Unsupported LINQ Methods" seems to answer my question. It not supported which is a good reason for getting this specific exception :) Thanks,Aharale
Aharon