简体   繁体   中英

How to solve “Exception has been thrown by the target of an invocation.” this error

The Program Works very well and produces output when I try to evaluate it throws me this error "Error in implementation. Exception has been thrown by the target of an invocation." What to do with this. Can anyone help me

Write a program to find the vehicles released between certain year.

\\class Vehicle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqApp1                
{
    public class Vehicle          
    {
        public String VehicleId{get; set; }
        public String VehicleName{ get; set; }
        public String Brand { get; set; }
        public int ReleaseYear { get; set; }

        public Vehicle(String vehicleId, String vehicleName, String brand,int releaseYear)
        {
            this.VehicleId = vehicleId;
            this.VehicleName = vehicleName;
            this.Brand = brand;
            this.ReleaseYear = releaseYear;
        }
        
    }
}

\\class Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


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
        {
            //Implement your code here
            //Vehicle v = new Vehicle();
            Console.WriteLine("Enter From Year:");
            int fromYear = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter To Year:");
            int toYear = int.Parse(Console.ReadLine());
            GetVehicleName(fromYear,toYear);
        }

        //Implement the method 'GetVehicleName' here
        public static void GetVehicleName(int fromYear, int toYear)
        {
            IEnumerable<Vehicle> vehicles = from vehicle in VehicleList where vehicle.ReleaseYear >= fromYear && vehicle.ReleaseYear <= toYear select vehicle; 
            Console.WriteLine("Vehicle Name Released Between {0} And {1}",fromYear,toYear);
            foreach(Vehicle v in vehicles)
            {
                Console.WriteLine(v.VehicleName);
            }
        }

         /** DO NOT CHANGE this ParameterExpression **/  
        public static ParameterExpression variableExpr = Expression.Variable(typeof(IEnumerable<Vehicle>), "sampleVar");
        
        public static Expression GetMyExpression(int fromYear, int toYear)
        {  
            Expression assignExpr = Expression.Assign(variableExpr,Expression.Constant(from vehicle in VehicleList where vehicle.ReleaseYear >= fromYear && vehicle.ReleaseYear <= toYear select vehicle.VehicleName/**copy the Linq query from the above method 'GetVehicleName' and paste it here**/));
            return assignExpr;
        }

    }
}

 
This is the sample input/output
Enter From Year: 2014
Enter To Year: 2016
Vehicle Name Released Between 2014 And 2016
Duster
Spacio
CRV
Nexon
Zest

Try this below Query,

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqApp1                
{
    public class Vehicle          
    {
        public String VehicleId{get; set; }
        public String VehicleName{ get; set; }
        public String Brand { get; set; }
        public int ReleaseYear { get; set; }

        public Vehicle(String vehicleId, String vehicleName, String brand,int releaseYear)
        {
            this.VehicleId = vehicleId;
            this.VehicleName = vehicleName;
            this.Brand = brand;
            this.ReleaseYear = releaseYear;
        }
        
    }
}

\\class Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


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)

                                };

        Public void Main(string[] args)   //DO NOT Change this 'Main' signature
        {
            //Implement your code here
            //Vehicle v = new Vehicle();
            Console.WriteLine("Enter From Year:");
            int fromYear = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter To Year:");
            int toYear = int.Parse(Console.ReadLine());
            GetVehicleName(fromYear,toYear);
        }

        //Implement the method 'GetVehicleName' here
        public static void GetVehicleName(int fromYear, int toYear)
        {
            var vehicles = (from vehicle in VehicleList where vehicle.ReleaseYear >= fromYear && vehicle.ReleaseYear <= toYear select vehicle).ToList().OrderBy(x=>x.ReleaseYear); 
            Console.WriteLine("Vehicle Name Released Between {0} And {1}",fromYear,toYear);
            foreach(Vehicle v in vehicles.ToList())
            {
                Console.WriteLine(v.VehicleName);
            }
        }

        

    }
}

 
This is the sample input/output
Enter From Year: 2014
Enter To Year: 2016
Vehicle Name Released Between 2014 And 2016
Duster
Spacio
CRV
Nexon
Zest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM