This can easily be done by using the Linq extension method Union. For example: var mergedList = list1. Union(list2).ToList() This will return a List in which the two lists are merged and doubles are removed.
If you don't specify a comparer in the Union extension method like in my example, it will use the default Equals and GetHashCode methods in your Person class. If you for example want to compare persons by comparing their Name property, you must override these methods to perform the comparison yourself. Check the following code sample to accomplish that.
You must add this code to your Person class summary> /// Checks if the provided object is equal to the current Person /// /// Object to compare to the current Person /// True if equal, false if not public override bool Equals(object obj) { // Try to cast the object to compare to to be a Person var person = obj as Person; return Equals(person); } /// /// Returns an identifier for this instance /// public override int GetHashCode() { return Name.GetHashCode(); } /// /// Checks if the provided Person is equal to the current Person /// /// Person to compare to the current person /// True if equal, false if not public bool Equals(Person personToCompareTo) { // Check if person is being compared to a non person. In that case always return false. If (personToCompareTo == null) return false; // If the person to compare to does not have a Name assigned yet, we can't define if it's the same.
Return false. If (string. IsNullOrEmpty(personToCompareTo.Name) return false; // Check if both person objects contain the same Name.
In that case they're assumed equal. Return Name. Equals(personToCompareTo.
Name); } If you don't want to set the default Equals method of your Person class to always use the Name to compare two objects, you can also write a comparer class which uses the IEqualityComparer interface. You can then provide this comparer as the second parameter in the Linq extension Union method. More information on how to write such a comparer method can be found on http://msdn.microsoft.com/en-us/library/system.collections.iequalitycomparer.aspx.
This can easily be done by using the Linq extension method Union. For example: var mergedList = list1. Union(list2).ToList(); This will return a List in which the two lists are merged and doubles are removed.
If you don't specify a comparer in the Union extension method like in my example, it will use the default Equals and GetHashCode methods in your Person class. If you for example want to compare persons by comparing their Name property, you must override these methods to perform the comparison yourself. Check the following code sample to accomplish that.
You must add this code to your Person class. /// /// Checks if the provided object is equal to the current Person /// /// Object to compare to the current Person /// True if equal, false if not public override bool Equals(object obj) { // Try to cast the object to compare to to be a Person var person = obj as Person; return Equals(person); } /// /// Returns an identifier for this instance /// public override int GetHashCode() { return Name.GetHashCode(); } /// /// Checks if the provided Person is equal to the current Person /// /// Person to compare to the current person /// True if equal, false if not public bool Equals(Person personToCompareTo) { // Check if person is being compared to a non person. In that case always return false.
If (personToCompareTo == null) return false; // If the person to compare to does not have a Name assigned yet, we can't define if it's the same. Return false. If (string.
IsNullOrEmpty(personToCompareTo. Name) return false; // Check if both person objects contain the same Name. In that case they're assumed equal.
Return Name. Equals(personToCompareTo. Name); } If you don't want to set the default Equals method of your Person class to always use the Name to compare two objects, you can also write a comparer class which uses the IEqualityComparer interface.
You can then provide this comparer as the second parameter in the Linq extension Union method. More information on how to write such a comparer method can be found on http://msdn.microsoft.com/en-us/library/system.collections.iequalitycomparer.aspx.
I don't see how this answers the question about the merge of values. – wdanda Oct 31 at 21:28.
There are a few pieces to doing this, assuming each list does not contain duplicates, Name is a unique identifier, and neither list is ordered. First create an append extension method to get a single list: static class Ext { public static IEnumerable Append(this IEnumerable source, IEnumerable second) { foreach (T t in source) { yield return t; } foreach (T t in second) { yield return t; } } } Thus can get a single list: var oneList = list1. Append(list2); Then group on name var grouped = oneList.
Group(p => p. Name); Then can process each group with a helper to process one group at a time public Person MergePersonGroup(IGrouping pGroup) { var l = pGroup.ToList(); // Avoid multiple enumeration. Var first = l.First(); var result = new Person { Name = first.Name, Value = first.
Value }; if (l.Count() == 1) { return result; } else if (l.Count() == 2) { result. Change = first. Value - l.Last().
Value; return result; } else { throw new ApplicationException("Too many " + result. Name); } } Which can be applied to each element of grouped: var finalResult = grouped. Select(g => MergePersonGroup(g)); (Warning: untested.
).
You need something like a full outer join. System.Linq. Enumerable has no method that implements a full outer join, so we have to do it ourselves.
Var dict1 = list1. ToDictionary(l1 => l1. Name); var dict2 = list2.
ToDictionary(l2 => l2. Name); //get the full list of names. Var names = dict1.Keys.
Union(dict2. Keys).ToList(); //produce results var result = names . Select( name => { Person p1 = dict1.
ContainsKey(name)? Dict1name : null; Person p2 = dict2. ContainsKey(name)?
Dict2name : null; //left only if (p2 == null) { p1. Change = 0; return p1; } //right only if (p1 == null) { p2. Change = 0; return p2; } //both p2.
Change = p2. Value - p1. Value; return p2; }).ToList().
I noticed that this question was not marked as answered after 2 years - I think the closest answer is Richards, but it can be simplified quite a lot to this: list1. Concat(list2) . ToLookup(p => p.Name) .
Select(g => g. Aggregate((p1, p2) => new Person { Name = p1.Name, Value = p1. Value, Change = p2.
Value - p1. Value })); Although this won't error in the case where you have duplicate names in either set. Some other answers have suggested using unioning - this is definitely not the way to go as it will only get you a distinct list, without doing the combining.
If you don't want to set the default Equals method of your Person class to always use the Name to compare two objects, you can also write a comparer class which uses the IEqualityComparer interface. You can then provide this comparer as the second parameter in the Linq extension Union method.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.