none
Getting a cannot convert error in linq query RRS feed

  • Question

  • Can anyone tell me why I'm getting this error:

    Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<ParetoRealEstate.Code.ListingObject>'. An explicit conversion exists (are you missing a cast?) C:\Users\chuck\Desktop\ParetoRealEstate\ParetoRealEstate\Code\ListingRepository.cs 16 24 ParetoRealEstate

    Here is my linq query and the object I'm setting it too:
    public IQueryable<ListingObject> AllListings()
        {
           return from listing in entities.Listings
                select new
                {
                  Listing = listing,
                  Amenity = (from am in entities.Amenities
                       where am.ListingID == listing.ListingID
                       select am),
                  Images = (from im in entities.ListingImages
                       where im.ListingID == listing.ListingID
                       select im)
                }; 
        }
    
    Here is the object that I'm setting it too:
     public class ListingObject
      {
        public Listing Listing { get; set; }
        public IQueryable<Amenity> Amenities { get; set; }
        public IQueryable<ListingImage> ListingImages { get; set; }
      }
    


    chuckdawit
    Sunday, April 24, 2011 5:42 PM

Answers

  • Lets try again;

    This part of your query :

    select new {

    Creates an Anonymous type and NOT a ListingObject as you want. For the query to return a IQueryable<ListingObject> you need to change the above select statement to this:

    select new ListingObject {

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by witdaj Sunday, April 24, 2011 7:12 PM
    Sunday, April 24, 2011 6:10 PM

All replies

  • Lets try again;

    This part of your query :

    select new {

    Creates an Anonymous type and NOT a ListingObject as you want. For the query to return a IQueryable<ListingObject> you need to change the above select statement to this:

    select new ListingObject {

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by witdaj Sunday, April 24, 2011 7:12 PM
    Sunday, April 24, 2011 6:10 PM
  • No where in your code do I see you accessing a column called "Image". On what line is the exception being thrown?
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, April 24, 2011 7:30 PM