Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit construction of entity type 'Artist' in query is not allowed

Compile just fine but execution fail with the error in the title.

ArtistService.cs

public class ArtistService : IArtistService
{
    public List<Artist>  ArtistDetail()  
    {
        using (ArtistDataContext db = new ArtistDataContext())
        {
            return (from artist in db.Artists

                select new Artist()
                {
                    Id = artist.Id,
                    Artist_name = Artist.Artist_name
                }).ToList();     <=== error happened here
        }
    }
}

code Behind

private List<ArtistServiceReference.Artist> ArtistDetail()
{
    ArtistServiceReference.ArtistServiceClient client = new 
    ArtistServiceReference.ArtistServiceClient();

    ArtistServiceReference.Artist[] artists = client.ArtistDetail();

    return artists.ToList();

I want to move the Artist List to a DropdownList.

The error is happening in the ArtistService.cs at the end {).ToList(); Any explanation on how to fix this issue? Thanks

I based my code on this example and this example is working fine.

example code MyService.cs

public class MyService : IMyService
{
    public List<Task> GetTasks()
    {
        using (TasksDataContext db = new TasksDataContext())
        {
            return (from task in db.TasksLists
                select new Task()
                {
                    Id = task.taskId,
                    Name = task.taskName,

                }).ToList();
        }
    }
}

Example default.aspx.cs

private List<TaskService.Task> GetTasks()
{
    TaskService.MyServiceClient client = new TaskService.MyServiceClient();

    TaskService.Task[] tasks = client.GetTasks();

    return tasks.ToList();
}

I don't understand why this example will work and not mine. The only difference was this example is returning to a gridview and I want to return to a dropdownlist.

like image 999
user3127986 Avatar asked Dec 25 '22 14:12

user3127986


1 Answers

Linq to Entities cannot translate the Artist object creation into SQL code (really, what is this supposed to look like?). Linq to Entities can only execute SQL queries and map returned fields to some entity to which it knows how to map (i.e. your DbSet entities). So, you need to execute the query first and then create the Artist entities locally:

public class ArtistService : IArtistService
{
    public List<Artist>  ArtistDetail()  
    {
        using (ArtistDataContext db = new ArtistDataContext())
        {
            return (from artist in db.Artists
                    select new { // select only columns you need
                       artist.Id,
                       artist.Artist_name
                    })
                    .AsEnumerable() // execute query
                    .Select(x => new Artist { // create instance of class
                        Id = x.Id,
                        Artist_name = x.Artist_name
                    })
                    .ToList();
        }
    }
}

BTW it looks like you have Artist entities in your Artists DbSet. Why not simply return

 return db.Artists.ToList();
like image 197
Sergey Berezovskiy Avatar answered May 05 '23 04:05

Sergey Berezovskiy