本文介绍了比较linq中的两个对象值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有和员工班



I have and Employee class

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 method

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"},
    };
}





比较两个清单,





Comparing two list,

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;
        }





我希望上面的ListCompare能够概括地使用任何对象之王,不仅适用于Employee,也适用于其他类(学生,人等)。请允许任何人帮助我吗?





显示结果,





I want the above ListCompare in generalize to use any king of objects not only for Employee, for other class also(Student, Person etc.). Pls can anyone help me?


Displaying Result,

<pre lang="c#">

列表< 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();

htmlTable.Append();

resultset = ListCompare(lt1,lt2);

foreach(结果集中的var项)

{

htmlTable.Append();

htmlTable.Append();

htmlTable.Append();

htmlTable.Append();

htmlTable.Append();

htmlTable.Append();

htmlTable.Append();

}

htmlTable.Append( > );

PlaceHolder1.Controls.Add(new Literal {Text = htmlTable.ToString()});





输出:

ID姓名年龄地址联系方式

----------- -------------------------------------------------- --------------------

1 $ $ $ $

2 Ravindran(已修改)30(已修改) $ $

3 Chandru(已修改)30(已修改)$ $

4 Rakesh(已修改)32(已修改)$ 9874021256(已修改)

5 Suresh(修改)$ 8541056387(修改)

11 Suryakala(已添加)28(已添加)No.1,Pillayar

koil Street,

Chennai(已添加)9541204782(已添加)

12 Thivya(已添加)41(已添加)No.42,Ellaiamman koil

街道,金奈(已添加) 9632140874(已添加)



我尝试过:



获得正确的输出,但我想以通用的方式将它用于任何类的对象,如学生,人等。

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("");
htmlTable.Append("");
resultset = ListCompare(lt1, lt2);
foreach(var item in resultset)
{
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("");
}
htmlTable.Append("IDNameAgeAddressContactNo" + item.ID + "" + item.Name + "" + item.Age + "" + item.Address + "" + item.ContactNo + "");
PlaceHolder1.Controls.Add(new Literal { Text = htmlTable.ToString() });


Output:
ID Name Age Address ContactNo
---------------------------------------------------------------------------------
1 $ $ $ $
2 Ravindran (Modified) 30 (Modified) $ $
3 Chandru (Modified) 30 (Modified) $ $
4 Rakesh (Modified) 32 (Modified) $ 9874021256(Modified)
5 Suresh (Modified) $ $ 8541056387(Modified)
11 Suryakala (Added) 28 (Added) No.1,Pillayar
koil Street,
Chennai (Added) 9541204782 (Added)
12 Thivya (Added) 41 (Added) No.42,Ellaiamman koil
Street,Chennai(Added) 9632140874 (Added)

What I have tried:

Getting correct output but i want in generalized way to use it for any class of objects like Student, Person etc.

推荐答案




这篇关于比较linq中的两个对象值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 23:36