简体   繁体   中英

Selecting DataRows into new structures using LINQ. Calling Distinct() fails

Consider these two structures:

struct Task
{
    public Int32 Id;
    public String Name;
    public List<Registration> Registrations;
}

struct Registration
{
    public Int32 Id;
    public Int32 TaskId;
    public String Comment;
    public Double Hours;
}

I am selecting a bunch of entries in a DataTable into new structures, like so:

var tasks = data.AsEnumerable().Select(t => new Task
{
    Id = Convert.ToInt32(t["ProjectTaskId"]),
    Name = Convert.ToString(t["ProjectTaskName"]),
    Registrations = new List<Registration>()
});

But when I call Distinct() on the collection, it doesn't recognize objects with the same values ( Id , Name , Registrations ) as being equal.

But if I use an equality comparer; comparing the Id property on the objects, it's all fine and dandy...:

class TaskIdComparer : IEqualityComparer<Task>
{
    public bool Equals(Task x, Task y)
    {
        return x.Id == y.Id;
    }


    public Int32 GetHashCode(Task t)
    {
        return t.Id.GetHashCode();
    }
}

What am I missing here? Is Distinct() checking something else than the value of properties?

LINQ's Distinct method compares objects using the objects' Equals and GetHashCode implementations.
Therefore, if these methods are not overridden, it will compare by reference, not by value.

You need to use an EqualityComparer . (Or implement Equals and GetHashCode for the Task class)

my guess is that it's the list in there. Almost certainly, the two list objects are different, even if they contain the same info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM