C# 在foreach循环之前检查员工姓名是否在列表中
因此,我试图检查输入的员工姓名是否等于列表中的员工姓名。我一辈子都想不出如何做到这一点 Program.csC# 在foreach循环之前检查员工姓名是否在列表中,c#,C#,因此,我试图检查输入的员工姓名是否等于列表中的员工姓名。我一辈子都想不出如何做到这一点 Program.cs private static void ReportPersonalUsage() { string employeename = ReadString("Employee Name:"); Manager_UI.ReportPersonalUsage(employeename); } 管理者 public static void Rep
private static void ReportPersonalUsage()
{
string employeename = ReadString("Employee Name:");
Manager_UI.ReportPersonalUsage(employeename);
}
管理者
public static void ReportPersonalUsage(string employeename)
{
List<Transaction> Transactions = StockManager.GetAllTransactions();
Console.WriteLine("\nPersonal Usage Log:");
Console.WriteLine("\t{0, -19} {1, -6} {2, -3} {3, -10} {4, -10}",
"Date Taken",
"Type",
"ID",
"Name",
"Employee");
foreach (Transaction transaction in Transactions)
{
if (transaction.GetEmployee() == employeename)
{
DisplayTransaction(transaction);
}
}
}
不过,我不知道该怎么做,因为有人可以帮忙,我对这一切都还不了解。publicstaticvoidreportpersonalusage(stringemployeename)
public static void ReportPersonalUsage(string employeename)
{
List<Transaction> Transactions = StockManager.GetAllTransactions();
bool isEmployeeExist = Transactions.Any(t => t.GetEmployee() == employeename);
if (isEmployeeExist == false)
{
Console.WriteLine(employeename + " Has Not Taken An Item From Stock Yet, Please Try Again");
}
else if (isEmployeeExist == true)
{
Console.WriteLine("\nPersonal Usage Log:");
Console.WriteLine("\t{0, -19} {1, -6} {2, -3} {3, -10} {4, -10}",
"Date Taken",
"Type",
"ID",
"Name",
"Employee");
foreach (Transaction transaction in Transactions)
{
DisplayTransaction(transaction);
}
}
}
{
列表事务=StockManager.GetAllTransactions();
bool isEmployeeExist=Transactions.Any(t=>t.GetEmployee()==employeename);
如果(isEmployeeExist==false)
{
Console.WriteLine(employeename+“尚未从库存中提取商品,请重试”);
}
else if(isEmployeeExist==true)
{
Console.WriteLine(“\n个人使用日志:”);
Console.WriteLine(“\t{0,-19}{1,-6}{2,-3}{3,-10}{4,-10}”,
“拍摄日期”,
“类型”,
“身份证”,
“姓名”,
“雇员”);
foreach(事务中的事务)
{
显示交易(transaction);
}
}
}
公共类事务
{
//事务获取/设置
公共项项{get;private set;}
公共字符串Employee{get;private set;}
公共字符串类型{get;private set;}
公共日期时间日期{get;private set;}
公共双价{get;私有集;}
//交易
公共事务(项目、字符串类型、字符串员工、日期时间日期、双项目价格)
{
项目=项目;
类型=类型;
雇员=雇员;
日期=日期;
价格=项目价格;
}
}
公共静态void ReportPersonalUsage(字符串employeeName)
{
IEnumerable transactions=StockManager.GetAllTransactions();
DisplayHeader();
foreach(事务中的事务)
{
if(transaction.Employee==employeeName)
{
显示交易(transaction);
}
}
}
私有静态void DisplayHeader()
{
Console.WriteLine(“\n个人使用日志:”);
Console.WriteLine(“\t{0,-19}{1,-6}{2,-3}{3,-10}{4,-10}”,
“拍摄日期”,
“类型”,
“身份证”,
“姓名”,
“雇员”);
}
私有静态无效显示事务(事务处理)
{
//添加显示逻辑
}
- 您应该使用IEnumerable来利用延迟加载。 仅在需要数据的位置迭代集合
- 在事务POCO中使用属性而不是方法
- 将私有属性设置为public,并将集合设置为private,如
public double Price{get;private set;}
Java
background:)只是说。顺便问一下,什么东西在代码中不起作用?您可以测试雇员是否存在于没有循环的列表中,例如:bool isEmployeeExist=Transactions.Any(t=>t.GetEmployee()==employeename)代码>@Mathews我知道!好眼力,并不是有些东西不起作用,更像是我想实现一个功能,但我不太确定该怎么做it@Sajid这看起来像是你应该知道的,任何(…)
下面都有一个循环,即使你没有写循环。如果您正在搜索一个大的列表,这一点可能很重要。您可以将if..else
替换为if(!isEmployeeExist){}else{}
public static void ReportPersonalUsage(string employeename)
{
List<Transaction> Transactions = StockManager.GetAllTransactions();
bool isEmployeeExist = Transactions.Any(t => t.GetEmployee() == employeename);
if (isEmployeeExist == false)
{
Console.WriteLine(employeename + " Has Not Taken An Item From Stock Yet, Please Try Again");
}
else if (isEmployeeExist == true)
{
Console.WriteLine("\nPersonal Usage Log:");
Console.WriteLine("\t{0, -19} {1, -6} {2, -3} {3, -10} {4, -10}",
"Date Taken",
"Type",
"ID",
"Name",
"Employee");
foreach (Transaction transaction in Transactions)
{
DisplayTransaction(transaction);
}
}
}
public class Transaction
{
//Transaction Get/Set
public Item Item { get; private set; }
public string Employee { get; private set; }
public string Type { get; private set; }
public DateTime Date { get; private set; }
public double Price { get; private set; }
//Transaction
public Transaction(Item item, string type, string employee, DateTime date, double itemprice)
{
Item = item;
Type = type;
Employee = employee;
Date = date;
Price = itemprice;
}
}
public static void ReportPersonalUsage(string employeeName)
{
IEnumerable<Transaction> transactions = StockManager.GetAllTransactions();
DisplayHeader();
foreach(Transaction transaction in transactions)
{
if (transaction.Employee == employeeName)
{
DisplayTransaction(transaction);
}
}
}
private static void DisplayHeader()
{
Console.WriteLine("\nPersonal Usage Log:");
Console.WriteLine("\t{0, -19} {1, -6} {2, -3} {3, -10} {4, -10}",
"Date Taken",
"Type",
"ID",
"Name",
"Employee");
}
private static void DisplayTransaction(Transaction transaction)
{
//add the display logic
}