简体   繁体   English

如何更新列表中的特定元素<t> ?</t>

[英]How can I update specific element in List<T>?

I am writing a C# console application where I have a class Customer as following:我正在编写一个 C# 控制台应用程序,其中我有一个 class 客户,如下所示:

    public class Customers
    {
        public string Name { get; set; }
        public string UserID { get; set; }
        public int Pin { get; set; }
        public int AccountNo { get; set; }
        public string AccountType { get; set; }
        public int Balance { get; set; }
        public string Status { get; set; }
    }

I am storing the customer data in text file as below ("Customers.txt"):我将客户数据存储在如下文本文件(“Customers.txt”)中:

[
    {"Name":"Jack Willson","UserID":"Jack21","Pin":12345,"AccountNo":1,"AccountType":"Savings","Balance":2000,"Status":"Active"},
    {"Name":"Mary Poppins","UserID":"mary8912","Pin":67890,"AccountNo":2,"AccountType":"Savings","Balance":4567,"Status":"Active"},
    {"Name":"Harry Potter","UserID":"Harry45","Pin":12345,"AccountNo":4,"AccountType":"Savings","Balance":12000,"Status":"Active"}
]

I am reading this file as:我正在阅读这个文件:

List<Customers> list = ReadFile<Customers>("Customers.txt");

I wish to update a specific customer field based on user input.我希望根据用户输入更新特定的客户字段。 If user leaves the field input blank then the field information remains unchanged.如果用户将字段输入留空,则字段信息保持不变。

The program will ask user to enter the AccountNo for which he wishes to update an information.该程序将要求用户输入他希望更新信息的AccountNo Then program will display each property like UserID , AccountType , Status .然后程序将显示每个属性,如UserIDAccountTypeStatus If the user doesnot enter any input for any of the property then information remains unchanged.如果用户没有为任何属性输入任何输入,则信息保持不变。 I was trying to save the input from user in new Customer() object but couldn't proceed to compare it or save it in List<customers> where i am storing the data from text file.我试图将用户的输入保存在new Customer() object 中,但无法继续比较它或将其保存在List<customers>中,我在其中存储来自文本文件的数据。 How can I achieve this?我怎样才能做到这一点?

Something like this should do the trick.像这样的东西应该可以解决问题。

var accountNum = 2; //I'm going to assume you get this from Console.ReadLine() and validate it.

//Find the customer with that account number...
var query = list.Where(c => c.AccountNo == accountNum);

if (!query.Any()
    Console.WriteLine("Customer not found.");
    
var customer = query.First();

//Now you can manipulate the customer object as you please
//In your situation I think you want to loop through the properties they can change and set them.
customer.Name = "Some new name";
customer.Pin = 1234;

//Save the updated information back to the text file however you currently do this

Hopefully this helps:)希望这会有所帮助:)

You can look into this solution using Linq:您可以使用 Linq 查看此解决方案

int acNo = ReadAccountNumber();
IEnumerable<Customers> results = myList.Where(c => c.AccountNo == acNo);

if you are sure that AccountNo are unique then results will hold just one element.如果您确定AccountNo是唯一的,那么results将只包含一个元素。 You can update the first element if that exists.如果存在,您可以更新第一个元素。

Customers cust = myList.First(c => c.AccountNo == acNo);
cust?.Name = newName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM