[英]Compare list data using Linq and C#
I have two lists of class, one called lsEmployee and another called lsAccount.我有两个类列表,一个叫做 lsEmployee,另一个叫做 lsAccount。 The account list is just a list of account names, wheras the employee list has employee and account.帐户列表只是帐户名称的列表,而员工列表包含员工和帐户。 I am attempting a linq query to see if the employee's account matches an account in the account list.我正在尝试 linq 查询以查看员工的帐户是否与帐户列表中的帐户匹配。
I am new to linq so it's likely just a syntax issue.我是 linq 的新手,所以这可能只是一个语法问题。 I have attempted the following code, but I cannot seem to get this to work.我尝试了以下代码,但似乎无法使其正常工作。 Any suggestions would be appreciated.任何建议,将不胜感激。
var lsEmployeeAccount = (lsAccount.Where(a => a.Account_Name == employee.Account_Name).SingleOrDefault()).ToString();
string lsEmployeeAccountString = lsEmployeeAccount.ToString() ?? "";
if (string.IsNullOrWhiteSpace(lsEmployeeAccountString))
{
//If account is not found, do stuff here
}
else
{
//If account is found, do stuff here
}
Not sure if you are looping through accounts or employees first, but assuming you are going through each employee:不确定您是先遍历帐户还是员工,但假设您要遍历每个员工:
foreach(var employee in lsEmployee)
{
var isExist = lsAccount.Contain(employee.Account_Name);
if(isExist)
{}
else
{}
}
You could do something like this:你可以这样做:
var lsEmployeeAccount = lsAccount.SingleOrDefault(a => a.Account_Name == employee.Account_Name);
var lsEmployeeAccountString = (lsEmployeeAccount != null) ? lsEmployeeAccount.ToString() : "";
if (string.IsNullOrWhiteSpace(lsEmployeeAccountString))
{
//If account is not found, do stuff here
}
else
{
//If account is found, do stuff here
}
But notice that lsEmployeeAccountString might not come out as you expect.但是请注意 lsEmployeeAccountString 可能不会像您预期的那样出现。
Consider using the SQL style LINQ syntax when joining multiple IEnumerable
s containing different kinds of data;连接包含不同类型数据的多个IEnumerable
时,请考虑使用 SQL 风格的 LINQ 语法; for example:例如:
var validAccounts = new[] {"1234", "2468", "1357", "6789", "9753", "9630"};
var employees = new[]
{
new Employee {Name = "Bob", Account = "1357"},
new Employee {Name = "Dave", Account = "1030"},
new Employee {Name = "Fred", Account = "6789"},
new Employee {Name = "Emma", Account = "2469"},
};
var employeesWithValidAccounts =
from emp in employees
join validAccount in validAccounts on emp.Account equals validAccount
select emp.Name;
foreach (var name in employeesWithValidAccounts)
{
Console.WriteLine(name);
}
Output:输出:
Bob
Fred
When you have SingleOrDefault
and there is no matching account then your lsEmployeeAccount
is null and you will get a NullReferenceException.当您有SingleOrDefault
并且没有匹配的帐户时,您的lsEmployeeAccount
为 null,您将收到 NullReferenceException。
Change your comparison to将您的比较更改为
var lsEmployeeAccount = lsAccount.Where(a => a.Account_Name == employee.Account_Name).SingleOrDefault()
if (lsEmployeeAccount == null)
{
//If account is not found, do stuff here
}
else
{
//If account is found, do stuff here
}
If you didn't need the account then another option would be to如果您不需要该帐户,则另一种选择是
if (!lsAccount.Any(a => a.Account_Name == employee.Account_Name))
{
//If account is not found, do stuff here
}
else
{
//If account is found, do stuff here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.