[英]Merging of 2 custom List objects in c#
Can someone help me out or can give me any pointer of solving this particular issue.有人可以帮助我,或者可以给我解决这个特定问题的任何指示。 Merging of list objects, by comparing 2 List of objects.
通过比较 2 个对象列表来合并列表对象。
It is manageable with simple for loops and linq, as data is huge I was searching for optimum way or any algorithm for this purpose would be of great help.它可以通过简单的 for 循环和 linq 进行管理,因为数据很大,我正在寻找最佳方式,或者为此目的的任何算法都会有很大帮助。
class user
{
string userID;
string userName;
//...
List<role> objRoles;
}
class role
{
List<pemission> objPermisison;
}
class pemission
{
bool newlyAdded;
//...
}
List<User> List1 :
User1
- Role_1
- Permission_1
- Permission_2
- Role_2
- Permission_3
- Permission_4
List<User> List2 :
User1
- Role_1
- Permission_3
- Permission_4
- Role_2
- Permission_5
- Permission_4
Required Result :
List<User> Resultant_List :
User1
- Role_1
- Permission_1
- Permission_2
- Permission_3 - Flag (Newly Added)
- Permission_4 - Flag (Newly Added)
- Role_2
- Permission_3
- Permission_4
- Permission_5 - Flag (Newly Added)
According to here to merge two lists without duplicates, a dictionary merge is the fastest method...根据here合并两个没有重复的列表,字典合并是最快的方法......
"This solution converts list2 to a dictionary and then it loops a single time over list1. It either adds to the dictionary or replaces the value of list2 if it was a duplicate. This seemed to be a much more efficient algorithm." “此解决方案将 list2 转换为字典,然后在 list1 上循环一次。它要么添加到字典中,要么替换 list2 的值(如果它是重复的)。这似乎是一种更有效的算法。”
var dict = list2.ToDictionary(p => p.userID);
foreach (var person in list1)
{
dict[person.userID] = person;
}
var merged = dict.Values.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.