C# Cognizant Handson Solutions

Vehicles Released in Certain Years – Linq C# Cognizant Handson

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

1. class Vehicle is already given for your reference.

2. class Program contains a hard-coded list with vehicle information in the ‘VehicleList’ attribute. 

a) Implement method ‘GetVehicleName’.

public static void GetVehicleName(int fromYear, int toYear)          //method signature

This method must display the vehicle names (from the given list) released between the fromYear and toYear (including these years).  USING LINQ CONCEPT

b) Implement method ‘Main’.

Get the “fromYear” and “toYear” from the user. Pass it to ‘GetVehicleName’ method.

NOTE : NEED NOT call ‘GetMyExpression’ method in Main.

c) method ‘GetMyExpression’

public static Expression GetMyExpression(int fromYear, int toYear)  

This method snippet is already given for you. THIS METHOD IS FOR TESTING YOUR LINQ QUERY EXPRESSION.  So fill your query expression in the space holder provided. ONLY THE QUERY EXPRESSION.  Nothing more need to be implemented in this method.

Vehicle.cs

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

Program.cs

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
            Console.WriteLine("Enter From Year : ");
            int fromYear = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Enter To Year : ");
            int toYear = Int32.Parse(Console.ReadLine());
            Console.WriteLine($"Vehicle Name Released Between {fromYear} And {toYear} : ");
            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;
            foreach(Vehicle vehicle in vehicles)
            Console.WriteLine(vehicle.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));
            return assignExpr;
        }

    }
}

Similar Posts