Click here to Skip to main content
15,916,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting this error for - "select v.VehicleName;"

What I have tried:

namespace LinqApp1                //DO NOT CHANGE the namespace name
{
    
    public class Program         //DO NOT CHANGE the class name
    {
        /** DO NOT CHANGE this 'List' declaration with initialized values  **/
        public static List<Vehicle> vehicleList = new List<Vehicle>()
                                {
                                    new Vehicle("HO345","CRV","Honda",2015),
                                    new Vehicle("HY562","Creta","Hyundai",2017),
                                    new Vehicle("RE198","Duster","Reanult",2014),
                                    new Vehicle("MA623","Spacio","Suzuki",2014),
                                    new Vehicle("TR498","Nexon","Tata",2015),
                                    new Vehicle("TR981","Zest","Tata",2016),
                                    new Vehicle("HO245","WRV","Honda",2018)

                                };

        static void Main(string[] args)   //DO NOT Change this 'Main' signature
        {
            int fromy,toy;
            Console.Write("Enter From Year : ");
            fromy = int.Parse(Console.ReadLine());
            Console.Write("Enter To Year : ");
            toy = int.Parse(Console.ReadLine());
            getVehicleName(fromy,toy);
        }

        public static void getVehicleName(int fromYear, int toYear)
        {
            IEnumerable<int> L = from v in vehicleList
            where v.ReleaseYear >= fromYear && v.ReleaseYear <= toYear
            select v.VehicleName; 
            
        }
Posted
Updated 18-Mar-20 12:03pm

1 solution

You can't cast a collection of strings to a collection of integers, any more than you can treat a shop full of Ferraris as a shop full of Harley Davidsons. They aren't the same thing at all, and you can't treat one like the other.

And in your case it's even worse because the actual values you would need to convert are not in any way treatable as numbers, unless all your vehicle names are "7" or "980" - they aren't numeric values: "CRV", "Duster", "Zest", ...

There is another couple of things though - your method getVehicleName which tries to do it doesn't return anything, so it's all thrown away anyway! But that doesn't matter because your code that calls it from Main doesn't use any return value anyway...

You need to stop panicking and throwing code together, and sit down and think about what you are trying to achieve, and the work out how you need to do that. Jumping straight into code isn't a good idea, and it shows in the result every time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900