Как решить «Исключение было сгенерировано целью вызова». эта ошибка

#c# #linq #invocationtargetexception

#c# #linq #исключение invocationtargetexception

Вопрос:

Программа работает очень хорошо и выдает выходные данные, когда я пытаюсь оценить ее, выдает мне эту ошибку «Ошибка в реализации. Исключение было сгенерировано целью вызова «. Что с этим делать. Кто-нибудь может мне помочь

Напишите программу для поиска транспортных средств, выпущенных между определенным годом.

\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 amp;amp; 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 amp;amp; 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
 

Комментарии:

1. Посмотрите на InnerException

2. Не могли бы вы поделиться с нами, какая строка выдает это исключение?

Ответ №1:

Попробуйте следующий запрос,

  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 amp;amp; 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
 

Комментарии:

1. Все то же исключение выдается «Ошибка в реализации. Исключение было сгенерировано целью вызова «.