score:3
As for new entries in the dictionary and new objects within an entry's List<object>
:
List<object>[] addedLists = New.Keys.Except(Existing.Keys)
.Select(key => New[key])
.ToArray();
object[] addedObjects = Existing.Keys.Intersect(New.Keys)
.SelectMany(key => New[key].Except(Existing[key])
.ToArray();
But the last requirement is a bit unclear. How would you define a changed object? Which objects should be compared for change? Comparing any object with any other object would potentially be defined as different, so there must be some similarities on which to base a comparison (e.g. the objects with the same value in their ID
property).
EDIT: As you clarified in your comment how object identity is defined, here's how to find all changed objects (assuming the dictionary's value is a list of Foo
that has a properties Name
and Type
that identify the object and property Value
that may change:
var differences = Existing.Keys.Intersect(New.Keys).SelectMany(key =>
from existingObj in Existing[key]
join newObj in New[key] on new { existingObj.Name, existingObj.Type } equals
new { newObj.Name, newObj.Type }
where existingObj.Value != newObj.Value
select new { Key = key, Existing = existingObj, New = newObj });
This will produce a sequence of objects each containing the key of the dictionary the difference was found in, the existing object and the new object. Identical objects will not be included in the results.
score:0
Dictionary<string, int> New = new Dictionary<string, int>();
Dictionary<string, int> Existing = new Dictionary<string, int>();
New.Add("A", 100);
New.Add("B", 200);
New.Add("Y", 300);
New.Add("X", 400);
Existing.Add("A", 1);
Existing.Add("B", 2);
Existing.Add("C", 3);
Existing.Add("D", 4);
Existing.Add("E", 5);
Existing.Add("F", 6);
Existing.Add("G", 7);
Existing.Add("H", 8);
var newStuff = New.Where(n => !Existing.ContainsKey(n.Key)).ToList();
var updatedStuff = Existing.Where(e => New.ContainsKey(e.Key) && e.Value != New.Single(n => n.Key == e.Key).Value);
newStuff.Dump();
updatedStuff.Dump();
//updated and new
newStuff.AddRange(updatedStuff);
newStuff.Dump();
Done in Linqpad.
Source: stackoverflow.com
Related Articles
- LINQ - Comparing dictionaries of lists to find new and changed objects
- Linq select objects by comparing from two lists of type int
- Comparing child lists of objects using Linq and C#
- LINQ Query - Find objects in a list who's lists contain objects with a certain property
- Using LINQ to Objects to find items in one collection that do not match another
- Linq find differences in two lists
- Find child objects in list of parent objects using LINQ
- LINQ Comparing Two Lists - Add new, remove old, leave the ones in common
- Linq - merging sub lists from different objects into a single object
- compare lists and return common objects using LINQ
- LINQ Source Code Available
- Linq to compare two lists of objects where one object has several lists
- LINQ for comparing two lists with complex entities
- Comparing Byte Arrays in LINQ to Objects
- C# / LINQ fastest way of comparing two lists and assigning value
- Linq: find similar objects from two different lists
- Comparing 2 lists using Linq
- LINQ Find Null Objects in List
- Comparing two lists using linq to sql
- Linq to objects find index and sum results
- Use LINQ to find complex combinations of items in two lists
- LINQ to Object comparing two lists of integer for different values
- LINQ to SQL: Advanced queries against lists, arrays and lists of objects
- Linq query to find duplicate objects based on multiple fields AND property is null
- Entity Framework - Linq - Comparing Nullable objects - NotSupportedException
- List of EF objects to Dictionary of lists using linq
- creating Linq to sqlite dbml from DbLinq source code
- LINQ Join / Union two lists of objects taking precedence from second over first
- LINQ How to return common objects across multiple lists
- Find common items in multiple lists in C# linq
- C# datagridview from linq
- Join between two repositories in Entity Framework Core
- Using reflection throws error in EF Core 3
- Why I see so much code duplication in Linq in Reflector?
- how to give DTO to LINQ select many query in c#
- List Iteration to Create Custom List in C#(Linq)
- C# linq grouping with same name
- How to mock SqlQuery
- aggregation count in linq query returns null
- Unable to cast object of type 'WhereEnumerableIterator`1' to type 'System.Collections.Generic.ICollection`1
- Compare input double with datatable string column values within a range using between query(SQL query to Linq on datatable c# )
- Why is RemoveAll(x => x.Condition) removing all my records?
- LINQ and 2 datatables
- Converting TSQL to Linq
- LINQ to Entities does not recognize the method
- Remove and reorder list elments
- Linq Left Join with Where clause
- How to execute an update on multiple rows using Linq Lambda Expression in ASP.Net MVC?
- Check if a list of integers increments by one
- Connect to Database with Connection String to use LINQ