Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For example, i got a .txt file in which there are cars names, amount of each car, year they were made and their price:
5
ORION; 2; 1935; 365.1;
POLARIS; 1; 1988; 87.09;
INDUSTRIERAD; 10; 1995; 58.10;
Bauer; 25; 2008; 285.58;
HERREN; 15; 2012; 1040.42;

I find the max value with for cycle that is 1040.42. So next i need to find the max values name(Herren), the amount of of that car that there is and the year it was made.
So the problem is i don't know how to find all of this. I need to use "for" and "if", but havo no idea how to do it.

What I have tried:

Everything that is on the internet.
Posted
Updated 4-Oct-18 3:43am

Have a look at this CodeProject article: C# - Light and Fast CSV Parser[^].
 
Share this answer
 
First create a POCO that represents according to your data:
C#
public class Car
{
	public string Name { get;set;}
	public int Amount { get;set;}
	public int Year { get;set;}
	public decimal Price { get;set;}
	
	public override String ToString()
	{
		return String.Format("Name:{0},Amount{1},Year:{2},Price:{3}",Name,Amount,Year,Price);
	}
}


and then read the file and then create a collection and then use linq to get the car with highest price and print it out:

C#
var lines = File.ReadAllLines("cars.text");

int totalCars = Convert.ToInt32(lines[0]);
		List<Car> cars = new List<Car>();
		for(int i = 1; i < totalCars; i++)
		{
			var line = lines[i];
			var carInfo = line.Split(';');
			
			Car car = new Car();
			car.Name = carInfo[0];
			car.Amount = Convert.ToInt32(carInfo[1]);
			car.Year = Convert.ToInt32(carInfo[2]);
			car.Price = Convert.ToDecimal(carInfo[3]);
			
			cars.Add(car);
		}
		
		var carWithHighestPrice = cars.OrderByDescending(x=>x.Price).First();
		
		Console.WriteLine(carWithHighestPrice);
 
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