Unable to construct a type constant value (type) In this environment, only primitive types ('such as Int32, String, and Guid') are supported.

entity-framework linq linq-to-entities many-to-many

Question

I've read everything:

I continued to look, but there was still no answer. This has been shown to occur on EF 3.5, and in EF 4.0 theContains method ought to be supported, but I'm receiving this problem with EF 4. I have a picture gallery where each photo may be a part of any number of albums and each album can have any number of different photographs. Thus, this connection is one of many to many.

I possess aVisibleObjects I'm copying the following property since it is used by approximately 100 other ways that are effective but I am certain that it is the root of the issue.

static IQueryable<GlobalObject> VisibleObjects
    {
        get
        {
            return from obj in db.GlobalObjectSet where obj.IsVisible && !obj.SiteUser.IsDeactivated orderby obj.ID descending select obj;
        }
    }

I've tried a variety of queries:

I possessVisiblePhotos property:

This was ineffective:

static IQueryable<Photo> VisiblePhotos(this Album a)
    {
        return from p in VisibleObjects.OfType<Photo>() where a.Photos.Contains(p) select p;
    }

This was modified to:

static IQueryable<Photo> VisiblePhotos(this Album a)
    {
        return from p in VisibleObjects.OfType<Photo>() where a.Photos.Any(other => p.ID == other.ID) select p;
    }

Still didn't function.

The calling method is as follows:

public static List<Photo> GetLatestPhotosByAlbum(Album alb, int count = 3)
    {
        lock (sync)
        {
            return alb.VisiblePhotos().OrderByDescending(p => p.ID).Take(count).ToList();
        }
    }

Wasn't functioning, therefore this was changed:

public static List<Photo> GetLatestPhotosByAlbum(Album alb, int count = 3)
    {
        lock (sync)
        {
            return (from p in VisibleObjects.OfType<Photo>()
                    where alb.Photos.Any(ph => ph.ID == ph.ID)
                    select p).ToList();
        }
    }

Still not functioning. If it helps, I'm complaining that I can't make a constant of my Photo object type, which is an Entity object with an ID attribute. I don't know what the actual reason of the problem is, and I don't have any additional questions or theories. The technique name, in my opinion, very well explains itself: I'm attempting to access the images in a certain album. It is not a solution to load album items into memory; the query should execute on the database, not memory. I need an explanation of this error, why it's happening here, and instructions on how to make my query function.

1
11
5/23/2017 12:16:38 PM

Accepted Answer

You want to utilize local Album in your linq-to-entities query, thus it won't work. To get its album, you must either utilize p's navigation property or:

var query = from p in VisibleObjects.OfType<Photo>()
            where p.Album.Id == alb.Id
            select p;

Perhaps you need to create a sophisticated query that joins albums and photographs. Local objects and any connections to the query cannot be passed. Passing just basic attributes is permitted.

12
8/28/2011 12:39:53 PM

Popular Answer

I believe EF is attempting to convert.where a.Photos.Contains(p) into SQL similarWHERE p IN (a.Photos) yet it lacks the language to do so.a.Photos in SQL. The SQL you want most likely looks likeWHERE p.Id IN (1, 2, 3) You may try doing it in C#:

static IQueryable<Photo> VisiblePhotos(this Album a)
{
    var photoIds = a.Photos.Select(p => p.Id).ToArray();
    return from p in VisibleObjects.OfType<Photo>() where photoIds.Contains(p.Id) select p;
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow