如何比较两个列表并返回3个分类列表 [英] How do I compare two lists and return 3 categoried lists
本文介绍了如何比较两个列表并返回3个分类列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表具有相同的属性,我想比较新旧数据库之间的数据
i have tow lists have the same properties and i want to compare the data between old and new DB
List<srsEmployee> srsEmps = db.srsEmployees.ToList(); //from old DB
List<destEmployee> destEmps = db2.destEmployees.ToList(); //from new DBs
i需要在视图中显示三个列表
1.新增员工
2.Deleted Employee
3.Common Employee
所以任何人都可以帮助我吗?
此查询我写的但结果不对
我尝试过:
i need Three list shown in the view
1.Newly Added Employee
2.Deleted Employee
3.Common Employee
so anyone can help me ?
this query what i wrote but the result isn't right
What I have tried:
var Common = destEmps.Where(n => srsEmps.Any(o => o.EmpCode == n.EmpCode)).ToList();
var Deleted = srsEmps.Where(o => !destEmps.Any(n => n.EmpCode == o.EmpCode)).ToList();
var NewlyAdded = destEmps.Where(n => !srsEmps.Any(o => o.EmpCode == n.EmpCode)).ToList();
<pre>
推荐答案
我并不自称善于使用LINQ,但确实给了它一个机会概念证明,它可以按照我的意图运作。
这不是100%的解决方案,但应该是一个很好的基础使用。
I do not profess to being adept with LINQ, but did give it a shot with this Proof-of-Concept and it works as I would intend it to.
It is not a 100% solution for you as is, but should be a good base to work with.
namespace ProofOfConcept {
public class Employee {
public int EmpCode { get; set; }
public string EmpName { get; set; }
public Employee() { }
public Employee(int Code, string Name) {
EmpCode = Code;
EmpName = Name;
}
}
class Program {
static void Main(string[] args) {
List<Employee> srsEmps = new List<Employee>();
srsEmps.Add(new Employee(1, "Me"));
srsEmps.Add(new Employee(2, "You"));
List<Employee> destEmps = new List<Employee>();
destEmps.Add(new Employee(2, "You"));
destEmps.Add(new Employee(3, "Someone Else"));
var Deleted =
from s in srsEmps
where !(from d in destEmps select d.EmpCode).Contains(s.EmpCode)
select s;
var Common =
from s in srsEmps
where (from d in destEmps select d.EmpCode).Contains(s.EmpCode)
select s;
var NewlyAdded =
from d in destEmps
where !(from s in srsEmps select s.EmpCode).Contains(d.EmpCode)
select d;
foreach (Employee d in Deleted) {
Console.WriteLine("Deleted = " + d.EmpName);
}
foreach (Employee c in Common) {
Console.WriteLine("Common = " + c.EmpName);
}
foreach (Employee n in NewlyAdded) {
Console.WriteLine("New = " + n.EmpName);
}
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
}
这篇关于如何比较两个列表并返回3个分类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文