我正在嘗試對 C# 中的二維串列進行排序。我撰寫了代碼,可以讓我從 CSV 檔案中提取的資料創建一個串列,但是我被困在對它們進行排序的函式上。
該串列詳細說明了商店 ID、商品和價格,格式如下:
1,shirt,5
1,pants,3
2,hat,3
我試圖找到一種方法來獲取用戶輸入,比如“襯衫”并回傳商店的 ID 和價格。我該怎么做呢?
這是完整的代碼。
namespace Shop
{
public class ShopPick
{
private class Shop
{
public int ShopId { get; set; }
public Dictionary<string, decimal> Goods { get; set; }
}
private readonly List<Shop> _Shops = new List<Shop>();
public void ReadShopData(string filePath)
{
try
{
var records = File.ReadLines(filePath);
foreach (var record in records)
{
var data = record.Split(',');
var ShopId = int.Parse(data[0].Trim());
var Shop = _Shops.Find(r => r.ShopId == ShopId);
if (Shop == null)
{
Shop = new Shop { Goods = new Dictionary<string, decimal>() };
_Shops.Add(Shop);
}
Shop.ShopId = ShopId;
Shop.Goods.Add(data.Skip(2).Select(s => s.Trim()).Aggregate((a, b) => a.Trim() "," b.Trim()), decimal.Parse(data[1].Trim()));
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
var ShopPicker = new ShopPick();
ShopPicker.ReadShopData(
Path.GetFullPath(
Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, @"../../../../Shop_data.csv")
)
);
// Item is found in Shop 2 at price 6.50
var bestShop = ShopPicker.PickBestShop("gac");
Console.WriteLine(bestShop.Item1 ", " bestShop.Item2);
Console.WriteLine("Done!");
Console.ReadLine();
}
public Tuple<int, decimal> PickBestShop(string items)
{
string input = Console.ReadLine();
string[] choices = input.Split(',');
foreach(var Shop in _Shops)
{
}
return new Tuple<int, decimal>(0, 0);
}
}
}
PickBestShop 功能是我卡住的地方
uj5u.com熱心網友回復:
您可以使用Linq陳述句。以下代碼將為您IEnumerable提供單個產品 ID。當然,您需要檢查空串列和空值之類的東西。該代碼假設產品至少具有欄位Id和Name.
products.Where(product => product.Name == name).Select(product => product.Id)
uj5u.com熱心網友回復:
我根據您的問題所理解的內容進行了一些編輯。c:\temp\shop.csv 中的內容如下所示:
1,shirt,5
1,pants,3
2,hat,3
3,shirt,6
3,hat,2
4,pants,2
3,trousers,20
5,shirt,5
5,hat,2
這是編輯過的代碼:
static void Main(string[] args)
{
//string fileName = Path.GetFullPath(Path.Combine(
// AppDomain.CurrentDomain.BaseDirectory, @"../../../../Shop_data.csv")
// );
string fileName = @"c:\temp\shops.csv";
var ShopPicker = new ShopPick(fileName);
var sample = ShopPicker.PickBestShop("shirt, Hat");
foreach (var shop in sample)
{
Console.WriteLine($"ShopId:{shop.ShopId}, Product: {shop.Product}, Price: {shop.Price}");
}
}
public class ShopPick
{
private List<Shop> _Shops;
public class Shop
{
public int ShopId { get; set; }
public string Product { get; set; }
public decimal? Price { get; set; }
}
public ShopPick(string filePath)
{
var records = File.ReadLines(filePath)
.Select(line => line.Split(','))
.Select(line => new Shop
{
ShopId = int.TryParse(line[0], out int sId) ? sId : -1,
Product = line[1].Trim(),
Price = decimal.TryParse(line[2], out decimal p) ? p : (decimal?)null
});
this._Shops = records.ToList();
}
//public List<Shop> Shops { get { return this._Shops; } }
public IEnumerable<Shop> PickBestShop(string items)
{
List<Shop> shops = new List<Shop>();
//string input = Console.ReadLine();
var choices = items.Split(',').Select(p => p.Trim().ToLower());
foreach (var item in choices)
{
var _shops = this._Shops.Where(s => s.Product.ToLower() == item);
if (_shops.Any())
{
var minPrice = _shops.Min(s => s.Price);
shops.AddRange(_shops.Where(s => s.Price == minPrice));
}
}
return shops;
}
}
輸出:
ShopId:1, Product: shirt, Price: 5
ShopId:5, Product: shirt, Price: 5
ShopId:3, Product: hat, Price: 2
ShopId:5, Product: hat, Price: 2
如果這不是您的意思和/或您需要任何部分的解釋,請告知。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417525.html
標籤:
