c# - 使用linq动态比较两个对象列表

标签 c# asp.net linq-to-sql

Click to see the Output:

我有一个 Employee 类

public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public string Address { get; set; }
        public string ContactNo { get; set; }
    }

并有 fill 方法来填充列表

private static void FillEmployeeList(ref List<Employee> lt1, ref List<Employee> lt2)
        {
            lt1 = new List<Employee> {new Employee{ID=1,Name="Kavya",Age="24",Address="No.1,Nehru Street,Chennai",ContactNo="9874521456"},
            new Employee{ID=2,Name="Ravi",Age="24",Address="Flat No.25/A1,Gandhi Street,Chennai",ContactNo="9658745258"},
            new Employee{ID=3,Name="Lavnya",Age="30",Address="No.12,Shastri nagar,Chennai",ContactNo="5214587896"},
            new Employee{ID=4,Name="Rupa",Age="31",Address="No.23/5,Nehru Street,Chennai",ContactNo="9874521256"},
            new Employee{ID=5,Name="Divya",Age="32",Address="No.1/227,Nehru Street,Chennai",ContactNo="8541256387"},            
            };

            lt2 = new List<Employee> {new Employee{ID=1,Name="Kavya",Age="24",Address="No.1,Nehru Street,Chennai",ContactNo="9874521456"},
            new Employee{ID=2,Name="Ravindran",Age="30",Address="Flat No.25/A1,Gandhi Street,Chennai",ContactNo="9658745258"},
            new Employee{ID=3,Name="Chandru",Age="30",Address="No.12,Shastri nagar,Chennai",ContactNo="5214587896"},
            new Employee{ID=4,Name="Rakesh",Age="32",Address="No.23/5,Nehru Street,Chennai",ContactNo="9874021256"},
            new Employee{ID=5,Name="Suresh",Age="32",Address="No.1/227,Nehru Street,Chennai",ContactNo="8541056387"},
            new Employee{ID=11,Name="Suryakala",Age="28",Address="No.1,Pillayar koil Street,Chennai",ContactNo="9541204782"},
            new Employee{ID=12,Name="Thivya",Age="41",Address="No.42,Ellaiamman koil Street,Chennai",ContactNo="9632140874"},           
            };
        }

比较两个对象列表

protected List<Employee> ListCompare(List<Employee> lt1, List<Employee> lt2)
        {
            FillEmployeeList(ref lt1, ref lt2);
            List<Employee> lst = new List<Employee>();

            if (lt1.Count > 0 && lt2.Count > 0)
            {
                // Displaying Matching Records from List1 and List2 by ID

                var result = (from l1 in lt1
                              join l2 in lt2
                              on l1.ID equals l2.ID
                              orderby l1.ID
                              select new
                              {

                                  ID = l1.ID,
                                  Name = (l1.Name == l2.Name) ? "$" : (l2.Name + " (Modified)"),
                                  Age = (l1.Age == l2.Age) ? "$" : (l2.Age + " (Modified)"),
                                  Address = (l1.Address == l2.Address) ? "$" : (l2.Address + " (Modified)"),
                                  ContactNo = (l1.ContactNo == l2.ContactNo) ? "$" : (l2.ContactNo + " (Modified)")
                              }).ToList();

                // Displaying Records from List1 which is not in List2
                var result1 = from l1 in lt1
                              where !(from l2 in lt2
                                      select l2.ID).Contains(l1.ID)
                              orderby l1.ID
                              select new
                              {
                                  ID = l1.ID,
                                  Name = " Deleted",
                                  Age = " Deleted",
                                  Address = " Deleted",
                                  ContactNo = " Deleted"
                              };

                // Displaying Records from List1 which is not in List2
                var result2 = from l1 in lt2
                              where !(from l2 in lt1
                                      select l2.ID).Contains(l1.ID)
                              orderby l1.ID
                              select new
                              {
                                  ID = l1.ID,
                                  Name = l1.Name + " (Added)",
                                  Age = l1.Age + " (Added)",
                                  Address = l1.Address + " (Added)",
                                  ContactNo = l1.ContactNo + " (Added)"
                              };

                var res1 = result.Concat(result1).Concat(result2);

                foreach (var item in res1)
                {
                    Employee emp = new Employee();
                    //Response.Write(item + "<br/>");
                    emp.ID = item.ID;
                    emp.Name = item.Name;
                    emp.Age = item.Age;
                    emp.Address = item.Address;
                    emp.ContactNo = item.ContactNo;
                    lst.Add(emp);
                }
            }
            return lst;
        }

这里我调用compareList方法并返回结果并将其显示在html表格上。

List<Employee> lt1 = new List<Employee>();
                List<Employee> lt2 = new List<Employee>();
                List<Employee> resultset = new List<Employee>();
                //string value = "ID";
                StringBuilder htmlTable = new StringBuilder();
                htmlTable.Append("<table border='1'>");
                htmlTable.Append("<tr><th>ID</th><th>Name</th><th>Age</th><th>Address</th><th>ContactNo</th></tr>");
                resultset = ListCompare(lt1, lt2);
                foreach(var item in resultset)
                {
                    htmlTable.Append("<tr>");
                    htmlTable.Append("<td>" + item.ID + "</td>");
                    htmlTable.Append("<td>" + item.Name + "</td>");
                    htmlTable.Append("<td>" + item.Age + "</td>");
                    htmlTable.Append("<td>" + item.Address + "</td>");
                    htmlTable.Append("<td>" + item.ContactNo + "</td>");
                    htmlTable.Append("</tr>");
                }
                htmlTable.Append("</table>");
                PlaceHolder1.Controls.Add(new Literal { Text = htmlTable.ToString() });

我的问题是如何概括这种编码。我可能有任何类(class)(如员工或学生)。我想要编码只是我将把两个对象列表传递给 CompareMethod(为了比较方法我将传递任何类型的对象列表)这将返回列表作为结果。如何进行,请给出任何想法。

最佳答案

如果你对集合平等感兴趣,我想推荐下一个东西:

Determines whether two sequences are equal by comparing their elements by using a specified IEqualityComparer. MSDN

Verifies that two specified collections are equivalent. The assertion fails if the collections are not equivalent. MSDN

Produces the set difference of two sequences. MSDN

关于c# - 使用linq动态比较两个对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321094/

相关文章:

c# - UserControl 将 GridView 导出为 PDF

c# - 在 LINQ2SQL 插入后获取主键

c# - LINQ to SQL 中的错误,数据库上有空字符串

c# - 在 C# 中查找 sin/cos 曲线的最小值和最大值的最有效方法

c# - 无法通过快捷方式调用片段

c# - 如何阻止变量重置

c# - 在自引用表中预加载 Linq to SQL 实体

c# - 为什么我的循环不允许我的标签颜色改变?

asp.net - web.config - 自动生成发布版本

c# - 从 LDAP 查询用户组