Help: Convert Linq.DataTable to Generic.List

Answered Help: Convert Linq.DataTable to Generic.List

  • Saturday, November 10, 2012 2:26 PM
     
     

    Hello. Would anyone of you could help me solve this? I'm getting this error when casting 'carsList' variable to '_vehicleList'

    Unable to cast object of type 'System.Data.Linq.DataQuery to type 'System.Collections.Generic.List`1[VehicleDAOModel]'.

    Does the Linq.DataQuery has to match the Properties in VehicleDAOModel?

    My code:

        public class VehicleDAOModel
        {
            [Display(Name = "Id")]
            public int _Id { get; set; }

            [Required]
            [Display(Name = "Year")]
            public string _year { get; set; }

            [Required]
            [Display(Name = "Make")]
            public string _make { get; set; }

            [Required]
            [Display(Name = "Model")]
            public string _model { get; set; }

            [Required]
            [Display(Name = "Trim")]
            public string _trim { get; set; }
        }

                // LINQ TO SQL CODE

               var dataContext = new VehicleModelL2SQLDataContext();

                var carsList = from v in dataContext.Vehicles
                               select v;
                _vehicleList = (List<VehicleDAOModel>)carsList;

               public List<VehicleDAOModel> _vehicleList = new List<VehicleDAOModel>();


    Thank you in advance.

All Replies

  • Saturday, November 10, 2012 5:14 PM
     
     Answered Has Code

    Hi devtekguy;

    In your query you are returning objects of type Vehicles. In the statement that follows you are attempting to cast Vehicles to objects of VehicleDAOModel, which is not allowed.

    If you what a list of VehicleDAOModel you can do the following :

    // LINQ TO SQL CODE
    
    var dataContext = new VehicleModelL2SQLDataContext();
    // Please note that the values of v.xxx in the select statement may need to be modified to match the value in v to VehicleDAOModel
    var carsList = from v in dataContext.Vehicles
                   select new VehicleDAOModel {
                       _Id = v.Id,
                       _year = v.year,
                       _make = v.make,
                       _model = v.model,
                       _trim = v.trim 
                   }; 
                   
    var _vehicleList = carsList.ToList();

      


    Fernando (MCSD)

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

    • Marked As Answer by devtekguy Saturday, November 10, 2012 6:04 PM
    •  
  • Saturday, November 10, 2012 6:04 PM
     
     
    Thank you Fernando! You rock! I tried it and it worked!  - All the best to your brother.
  • Saturday, November 10, 2012 6:35 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".