Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic List Union Question

Tags:

c#

linq

generics

I'm trying to merge 2 lists using "Union" so I get rid of duplicates. Following is the sample code:

public class SomeDetail
{
    public string SomeValue1 { get; set; }
    public string SomeValue2  { get; set; }
    public string SomeDate { get; set; }
}

public class SomeDetailComparer : IEqualityComparer<SomeDetail>
{
    bool IEqualityComparer<SomeDetail>.Equals(SomeDetail x, SomeDetail y)
    {
        // Check whether the compared objects reference the same data.        
        if (Object.ReferenceEquals(x, y))
            return true;
        // Check whether any of the compared objects is null.        
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;
        return x.SomeValue1 == y.SomeValue1 && x.SomeValue2 == y.SomeValue2;
    }
    int IEqualityComparer<SomeDetail>.GetHashCode(SomeDetail obj)
    {
        return obj.SomeValue1.GetHashCode();
    }
}

List<SomeDetail> tempList1 = new List<SomeDetail>();
List<SomeDetail> tempList2 = new List<SomeDetail>();

List<SomeDetail> detailList = tempList1.Union(tempList2, SomeDetailComparer).ToList();

Now the question is can I use Union and still get the record which has the latest date (using SomeDate property). The record itself can either be in tempList1 or tempList2.

Thanks in advance

like image 869
Ganesha Avatar asked May 11 '09 19:05

Ganesha


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

The operation that is really suited to this purpose is an full outer join. The Enumerable class has an implementation of inner join, which you can use to find the duplicates and select whichever you prefer.

var duplicates = Enumerable.Join(tempList1, tempList2,  keySelector, keySelector, 
    (item1, item2) => (item1.SomeDate > item2.SomeDate) ? item1 : item2)
    .ToList();

keySelector is simply a function (could be a lambda expression) that extracts a key from an object of type SomeDetail. Now, to implement the full outer join, try something like this:

var keyComparer = (SomeDetail item) => new { Value1 = item.SomeValue1,
    Value2 = item.SomeDetail2 };
var detailList = Enumerable.Union(tempList1.Except(tempList2, equalityComparer), 
    tempList2.Except(tempList1, equalityComparer)).Union(
    Enumerable.Join(tempList1, tempList2, keyComparer, keyComparer
    (item1, item2) => (item1.SomeDate > item2.SomeDate) ? item1 : item2))
    .ToList();

equalityComparer should be an object that implements IEqualityComparer<SomeDetail> and effectively uses the keyComparer function for testing equality.

Let me know if that does the job for you.

like image 141
Noldorin Avatar answered Sep 20 '22 12:09

Noldorin


You'd have to be able to tell Union how to pick which one of the duplicates to use. I don't know of a way to do that other than writing your own Union.

like image 43
John Weldon Avatar answered Sep 18 '22 12:09

John Weldon


You cannot with the standard Union method, but you can create an extension method Union for List<SomeDetail> with this special handling and this method will be used because the signature fits better.

like image 29
Daniel Brückner Avatar answered Sep 20 '22 12:09

Daniel Brückner