Reputation: 41
so i have two linked lists: One which contains items that are required, and another linked list which has a list of items.
So linked list that contains items which are required outputs:
Required item: Automobilis | Amount needed: 1
Required item: Kirvis | Amount needed: 2
Required item: Piesiniai | Amount needed: 2
Another linked list which holds all the items that i have
Item name: Automobilis | Amount available: 1 | Price of item: 3000
Item name: Automobilis | Amount available: 1 | Price of item: 5000
Item name: Rubai | Amount available: 20 | Price of item: 80
Item name: Lemputes | Amount available: 3 | Price of item: 700
Item name: Piesiniai | Amount available: 1 | Price of item: 2000
Item name: Piesiniai | Amount available: 1 | Price of item: 1800
Item name: Kirvis | Amount available: 50 | Price of item: 100
I am trying to sort through the items that are required and the items that i have, see if the names match, if they match i want to add them to a new linked list.
My linked list looks like this
static void FormingNewList(LinkedList<Warehouse> house, LinkedList<Order> order, LinkedList<MatchingItems> match)
{
double min = house.First().Price;
int count = 0;
foreach (Order ord in order)
{
foreach(Warehouse store in house)
{
if(ord.Title == store.Title)
{
string nam = store.Title;
int am = store.Amount;
double price = store.Price;
int id = count++;
MatchingItems add = new MatchingItems(nam, am, price, id);
match.AddLast(add);
}
}
}
}
It outputs:
Name of item: Automobilis |Amount of said item available: 1 |Price of item: 3000
Name of item: Automobilis |Amount of said item available: 1 |Price of item: 5000
Name of item: Kirvis |Amount of said item available: 50 |Price of item: 100
Name of item: Piesiniai |Amount of said item available: 1 |Price of item: 2000
Name of item: Piesiniai |Amount of said item available: 1 |Price of item: 1800
How do i go about only finding lowest price of given item. So for example if i have 1 car which is worth 5000 and another worth 3000 i want to choose the lowest price.
So the desired output would be:
Item: Automobilis | Amount: 1 | Price: 3000
Item: Piesiniai | Amount: 1 | Price: 1800
Item: Kirvis | Amount: 50 | Price: 100
Upvotes: 0
Views: 65
Reputation: 2153
If I understand, you want to group the products by Name then select the lowest price of that item. You can use GroupBy
to separate the list by item Name, then Select
the first item from the group after ordering by price (low to high).
var SmallestValues = Items.GroupBy(i => i.Name)
.Select(group => group.OrderBy(x => x.Price).FirstOrDefault());
Upvotes: 1