I am trying to merge two lists into one List without duplicates
JOIN operator returns only common elements
These are lists in JSON
List1 is:
{
"screenID": 96,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": true
},
{
"screenID": 97,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": true
},
{
"screenID": 98,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": true
}
List2 is:
{
"screenID": 96,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": false
},
{
"screenID": 97,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": false
}
If ScreenID is same then I want to compare between CRUD elements like:
if(ScreenID == 96){
Create = List1.Create == true && List2.Create == false ? true : false
}
I tried this :
var finalList = list1.Union(list2);
but the result was:
{
"screenID": 96,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": true
},
{
"screenID": 97,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": true
},
{
"screenID": 98,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": true
},
{
"screenID": 96,
"create": true,
"read": true,
"update": true,
"delete": true,
"print": false
},
{
"screenID": 97,
"create": true,
"read": true,
"update": true,
"delete": true,
I am beginner in LINQ so any help is appreciated
EDIT
I am using .NET 3.1
3
Answers
.NET 6 introduced UnionBy which can be used to combine two collections based on an expression. When duplicates are found, the method returns the first item it encounters. The two collections should be ordered first to control which item is returned :
For more complex logic, the order expression should be modified to ensure the desired item comes first. For example, to select the item with the most flags,
Enumerable.Count(t=>t)
can be used :Using DistinctBy
In previous versions .NET versions the MoreLINQ library can be used. There’s no
UnionBy
butDistinctBy
can be used for the same job after concatenation. Once again, the order controls what is returned :DistinctBy is also available in .NET 6.
EDIT: I’ve made some changes to adapt my original answer to .Net 3.5, this should now work provided you’re working with C# 7.3 or later (Set by
<LangVersion>7.3</LangVersion>
in your .csproj file). I ditched theHashSet<T>
for aDictionary<T>
which actually made the whole thing less verbose.While the exising answer is good I’d like to add an alternate approach using an extension method that essentially should do the same as
UnionBy
but accept an additonalFunc<T, T, T>
for handling duplicates.TheUnionComparer
makes it a bit verbose but that essentially just wraps theIEqualityComparer<TKey>
for the key selector.I’ve made a simple example class for your data:
Usage:
EDIT: I just saw you are limited to .Net 3.5, I’m not entirely sure you will not have to make any changes to this butHashSet<T>
andIEqualityComparer<T>
should be supportedWith class
This simple union works: