Reputation: 457
//create the new object for cars
Cars s1 = new Cars("Toyota", 2005, 500000, "White", "good");//Car1 Ob
Cars s2 = new Cars("Honda", 2004, 550000, "Black", "fine");//Car2 Ob
Cars s3 = new Cars("Nissen", 2012, 490000, "Yellow", "best");//Car3 Ob
Cars s4 = new Cars("Suzuki", 2012, 390000, "Blue", "fine");//Car4 Ob
Cars s5 = new Cars("BMW", 2012, 1000000, "Green", "Good");//Car5 Ob
//Create list to add objects into the memory
List<Cars> list1 = new List<Cars>();
list1.Add(s1);list1.Add(s2);list1.Add(s3);list1.Add(s4);list1.Add(s5);
//cars info which has the lowest price
double lowest_price = 0;
foreach(Cars a in list1){
if(a.price <= lowest_price){
lowest_price = a.price;
Console.WriteLine(a.price);
}
}//end of loop
That's the code which I am trying to print out the the cars info which has the lowest price. But nothing gets print out.
Upvotes: 41
Views: 128508
Reputation: 41
var result = list1.Where((x) => x.price== list1.Min(y => y.price)).ToList();
This will return car info with the lowest price. Add reference using System.Linq;
Upvotes: 3
Reputation: 19
public static int FindMin(IEnumerable<int> numbers)
{
Expression<Func<int, int, int>> expression = (left, right) => left < right ? left : right;
InvocationExpression invoker = Expression.Invoke(expression,
Expression.Constant(numbers.First())
, Expression.Constant(numbers.ToList()[1]));
foreach (int number in numbers.Skip(2))
{
invoker = Expression.Invoke(expression,
invoker,
Expression.Constant(number));
}
var lambdaExpression = Expression.Lambda<Func<int>>(invoker);
Console.WriteLine($"Expression call tree:{lambdaExpression.ToString()}");
return lambdaExpression.Compile().Invoke();
}
}
Upvotes: 1
Reputation: 8786
just based on the problem with your code: you are not going to have a lower price than 0... so you need change it to:
double lowest_price = list1[0].price;
foreach(Cars a in list1){
if(a.price <= lowest_price){
lowest_price = a.price;
Console.WriteLine(a.price);
}
}//end of loop
Edit: this will only work if list1
exists and isn't empty, for general use you'd need to check if (list1 is null || list1.Count==0)
first line.
Upvotes: 4
Reputation: 3123
If you want to fix your code to work (instead of using Linq - which is the recommended approach), change this line:
double lowest_price = 0;
to this:
double lowest_price = double.MaxValue;
Upvotes: 5
Reputation: 23208
Use the LINQ Min
extension method:
double lowest_price = list1.Min(car => car.price);
Also, you didn't specify, but this will fail if you have no cars in your set with an InvalidOperationException
indicating "Sequence contains no elements". If it's possible you have no cars, a quick update might be:
double lowest_price = list1.Any() ? list1.Min(car => car.price) : 0;
As to why your current code prints nothing it's because your initial value is 0
. No car has a value that is negative (or less than 0
). If you want to keep using your existing loop, change the initial value to the highest possible value:
double lowest_price = Double.MaxValue;
foreach(Cars a in list1){
if(a.price <= lowest_price){
lowest_price = a.price;
Console.WriteLine(a.price);
}
}//end of loop
Note that this has the additional side effect that if your list1
of cars is empty, then the lowest_price
value will be Double.MaxValue
. This may or may not be a concern for you with your existing code.
If it is a concern, and need to return 0
if there are no cars, you can make a slight adjustment:
double lowest_price;
if (list1.Any()){
lowest_price = Double.MaxValue;
foreach(Cars a in list1){
if(a.price <= lowest_price){
lowest_price = a.price;
Console.WriteLine(a.price);
}
}//end of loop
}
else{
lowest_price = 0;
}
Upvotes: 81
Reputation: 537
The other responses correctly provide a LINQ solution, but the issue with your specific code is that you are checking if the car's priced (a.price) is <= your lowest_price variable. Your lowest_price variable is instantiated with a value of 0 and according to your default car prices listed which are all greater than 0, will never validate. So your lowest_price variable will never be updated and therefore never write it's value to the console. That's what's causing your request of "nothing prints out". It's an error in your check and in your logic. Update that line to "if (lowest_price <= a.price)" to get closer.
Upvotes: 1
Reputation: 1335
You would use the Min extension on the list.
lowest_price = list1.Min(c => c.price);
Upvotes: 16