none
Issues casting from LINQ query to generic list RRS feed

  • Question

  • Here is my 1st attempt of this query:

    var artists =
        (from Artist in _context.Artists
            join Genre in _context.Genres on Artist.GenreId equals Genre.GenreId
            where
                Artist.Deleted == false &&
                Genre.Deleted == false
            group new { Artist, Genre } by new
            {
                Artist.ArtistName,
                Genre.GenreName,
                Artist.ArtistId,
                Artist.UserId
            } into g
            orderby
                g.Key.ArtistName
            select new
            {
                g.Key.ArtistName,
                g.Key.GenreName,
                ArtistID = (System.Int32?)g.Key.ArtistId,
                UserID = (System.Int32?)g.Key.UserId
            }).ToList();
    
    return artists.Cast<Artist>().ToList();

    When it runs it falls over at return artists.Cast<Artist>().ToList(); with Unable to cast object of type '<>f__AnonymousType2`4[System.String,System.String,System.Nullable`1[System.Int32],System.Nullable`1[System.Int32]]' to type 'HomeController.Domain.DAL.Artist'.

    I tried various attempts since including the suggestion here using IEnumerable instead of anonymous use of var. But I don't get the ToList() at then of the query when I tried it.

    Any help is much appreciated :)


    • Edited by Davearia Wednesday, February 27, 2013 3:46 PM
    Wednesday, February 27, 2013 3:46 PM

Answers

  • Hi Davearia;

    A collection of Anonymous type which is what the variable artists is holding can not be cast to a collection of Artist type, they do not have the same inheritance chain. What you can do is to create a Data Transfer Object and have the query fill that object as a collection and sent that to your controller. For example :

    public class ArtistDTO
    {
        public string ArtistName { get; set; }
        public string GenreName { get; set; }
        public Int32? ArtistId { get; set; }
        public Int32? UserId { get; set; }
    }
    
    Then in the last select clause of the query you can do this:
    
    select new ArtistDTO()
    {
        ArtistName = g.Key.ArtistName,
        GenreName = g.Key.GenreName,
        ArtistId = (System.Int32?)g.Key.ArtistId,
        UserId = (System.Int32?)g.Key.UserId
    }).ToList();
    
    The results of the query will now be a List<ArtistDTO> .

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Davearia Thursday, February 28, 2013 8:17 AM
    Wednesday, February 27, 2013 9:19 PM

All replies

  • Hi Davearia;

    A collection of Anonymous type which is what the variable artists is holding can not be cast to a collection of Artist type, they do not have the same inheritance chain. What you can do is to create a Data Transfer Object and have the query fill that object as a collection and sent that to your controller. For example :

    public class ArtistDTO
    {
        public string ArtistName { get; set; }
        public string GenreName { get; set; }
        public Int32? ArtistId { get; set; }
        public Int32? UserId { get; set; }
    }
    
    Then in the last select clause of the query you can do this:
    
    select new ArtistDTO()
    {
        ArtistName = g.Key.ArtistName,
        GenreName = g.Key.GenreName,
        ArtistId = (System.Int32?)g.Key.ArtistId,
        UserId = (System.Int32?)g.Key.UserId
    }).ToList();
    
    The results of the query will now be a List<ArtistDTO> .

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Davearia Thursday, February 28, 2013 8:17 AM
    Wednesday, February 27, 2013 9:19 PM
  • Creating a class like you suggested works, but why is it that if you use the class that Entity Framework generated from the underlying database I get this error?

    The entity or complex type 'HomeControllerModel.Artist' cannot be constructed in a LINQ to Entities query.

    I would have preferred not to not create additional classes and used the classes provided by Entity. That said if that is not possible then I very grateful for you help. Thank you very much :)


    Thursday, February 28, 2013 8:22 AM
  • Hi Davearia;

    As you have found out that you can not use the classes that were created by the Entity Framework to fill just a few fields. These classes are assigned to the data context and in order to use these classes in the data context it must not violate any of the relational constraint and the Primary key so that it can be tracked and the reason for the need for a Data Transfer Object in cases like this.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 28, 2013 3:35 PM
  • Hi Fernando,

    Thanks for answering this, as I said earlier your solution is perfect and it working a treat. I just wanted to know why this was an issue and your explanation is totally acceptable. Thank you again!

    Thursday, February 28, 2013 5:38 PM
  •   

    Not a problem, glad I was able to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 28, 2013 5:59 PM