所以我有两个链接列表:一个包含必需的项目,另一个包含项目列表。
包含所需输出项的链接列表:
Required item: Automobilis | Amount needed: 1
Required item: Kirvis | Amount needed: 2
Required item: Piesiniai | Amount needed: 2
另一个链接列表,其中包含我拥有的所有物品
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
我正在尝试对所需的项目和我拥有的项目进行排序,看看名称是否匹配,如果它们匹配,我想将它们添加到新的链接列表中。
我的链接列表如下
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);
}
}
}
}
它输出:
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
我该如何只查找给定项目的最低价格。因此,例如,如果我有一辆价值5000的汽车,而另一辆价值3000的汽车,我想选择最低的价格。
因此所需的输出将是:
Item: Automobilis | Amount: 1 | Price: 3000
Item: Piesiniai | Amount: 1 | Price: 1800
Item: Kirvis | Amount: 50 | Price: 100
答案 0 :(得分:1)
据我了解,您想按名称对产品进行分组,然后选择该产品的最低价格。您可以使用GroupBy
按项目名称分隔列表,然后按价格(从低到高)订购Select
从组中选择第一项。
var SmallestValues = Items.GroupBy(i => i.Name)
.Select(group => group.OrderBy(x => x.Price).FirstOrDefault());