所以我設定了 gui,以便我有兩個主要的串列框。我還在弄清楚我想要這個應用程式什么樣的 gui,所以還有另一個,但這不相關。一個串列框是您要為部門或員工檢查的選項串列。另一個是部門串列。現在我有名稱選項的功能來查看部門員工的姓名。我只需要知道如何過濾串列,以便在單擊提交按鈕后顯示的唯一員工是所選部門中的員工。我想我會為此使用 lambda 運算式,但它并沒有為我作業。我真的很想知道如何更好地使用 lambda 運算式,所以請只給我一個涉及使用它們的解決方案。如果它'
我放置讀取的檔案并將 dept 陣列設定為檔案內容
//list of employees
public static List<Employee> EmployeeList = new List<Employee>();
//array to hold the options users have for interacting with info
public static string[] OptionsArr;
//array to hold the departments
public static string[] DeptsArr;
//skipping around same file to relevant code
//set the departments array to the contents of the depts file
DeptsArr = File.ReadAllLines("..\\..\\departments.txt");
不確定是否需要 填充 DeptListBox 的方法
private void UpdateDeptListBox()
{
//set up for new info
DeptListBox.Items.Clear();
//prevent interfence with update
DeptListBox.BeginUpdate();
//set department listbox to depts array
DeptListBox.DataSource = Program.DeptsArr;
DeptListBox.EndUpdate();
}
問題方法 - 提交按鈕方法
List<Employee> ResultList = new List<Employee>();
//name
if (OptionsListBox.SelectedIndex == 1)
{
//user selects Marketing department
if (DeptListBox.SelectedIndex == 0)
{
//problem is either with lambda exp or Program.DeptsArr comparison
foreach (Employee empl in Program.EmployeeList.Where(empl => empl.Dept.CompareTo(Program.DeptsArr[0]) == 0).ToList())
{
//this doesn't happen
ResultList.Add(empl);
}
for (int i = 0; i<ResultList.Count; i )
{
ResultListBox.Items.Add(ResultList[i].Lname " " ResultList[i].Fname " " ResultList[i].Dept);
}
}
}
}
uj5u.com熱心網友回復:
對我來說,當我遇到分解東西并查看較小部分的問題時,它會很有幫助。您確定問題出在您的 lambda 函式上嗎?可能是您的選項串列框!= 1 或者資料未正確讀取。
據我所知,這部分應該可以作業。雖然它存在一些問題:
foreach (Employee empl in Program.EmployeeList.Where(empl =>empl.Dept.CompareTo(Program.DeptsArr[0]) == 0).ToList())
{
//this doesn't happen
ResultList.Add(empl);
}
您可以從 Employee lambda 函式開始,然后對值進行硬編碼。也許像這樣確實會產生正確的結果(鮑勃和布蘭登)
List<Employee> ResultList = new List<Employee>();
List<Employee> EmployeeList = new List<Employee> {
new Employee{ Name = "Bob", Dept = "Accounting" },
new Employee{ Name = "Larry", Dept = "A" },
new Employee{ Name = "Margret", Dept = "B" },
new Employee{ Name = "Brandon", Dept = "Accounting" }
};
string[] DeptsArr = new string[2];
DeptsArr[0] = "Accounting";
DeptsArr[1] = "A";
//user selects Marketing department
if (departmentIndex == 0)
{
foreach (Employee empl in EmployeeList.Where(empl => empl.Dept.CompareTo(DeptsArr[0]) == 0).ToList())
{
ResultList.Add(empl);
}
}
但是,您在 foreach 回圈中的 lamda 函式是多余的。您可以將 lambda 函式視為運行 foreach 回圈的指令。foreach 回圈本身可能如下所示:
List<Employee> ResultList = new List<Employee>();
foreach (Employee empl in EmployeeList)
{
if(empl.Dept == DeptsArr[0])
{
ResultList.Add(empl);
}
}
通過使用以下 lamda 函式,您可以獲得與上述 foreach 回圈相同的結果:
List<Employee> ResultList = EmployeeList.Where(empl => empl.Dept == DeptsArr[0]).ToList();
最后一點是,該 lambda 函式末尾的“ToList()”是執行回圈并將結果作為串列回傳的內容。很多時候這不是必需的。如果沒有“ToList()”部分,將回傳一個 IEnumerable,您可以使用它來代替。在許多情況下,使用 IEnumerable 而不是呼叫 ToList() 可以獲得更好的性能。
uj5u.com熱心網友回復:
如果要測驗特定值是否在陣列中,則呼叫Contains該陣列,例如
var allEmployees = new List<Employee>();
// Populate allEmployees here.
var selectedEmployees = allEmployees.Where(e => selectedDepartments.Contains(e.Department)).ToArray();
該selectedEmployees陣列將僅包含串列中具有包含在陣列/集合中的屬性值的Employee物件。allEmployeesDepartmentselectedDepartments
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/482881.html
