Linq Select Distinct with Anonymous Types

LINQ Select Distinct with Anonymous Types

Have a read through K. Scott Allen's excellent post here:

And Equality for All ... Anonymous Types

The short answer (and I quote):

Turns out the C# compiler overrides
Equals and GetHashCode for anonymous
types. The implementation of the two
overridden methods uses all the public
properties on the type to compute an
object's hash code and test for
equality. If two objects of the same
anonymous type have all the same
values for their properties – the
objects are equal.

So it's totally safe to use the Distinct() method on a query that returns anonymous types.

How does Distinct() work on a List of anonymous type?

From MSDN

Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.

Linq -How to get distinct of multiple fields from Anonymous type

If I understand your question correctly, you want a single list containing unique values from FromUserId and ToUserId fields. In that case you want to union lists of each ID type.

var userDetails = new List<userdetails>();
// fill list with values.

var values = userDetails.Select(i => i.FromUserID).Union(userDetails.Select(i => i.ToUserID));

Distinct() on anonymous object not working in LINQ

You are using DateTime.Now for Created/Updated, so it would be quite unlikely that values between entries would be the same, they most likely differ by fraction of millisecond. If you displayed the values with greater precision, eg. .Ticks, they would be different.

I think you should save DateTime.Now to variable before LINQ and use it, to ensure they are the same for all entries. Other option is to add it to the object after .Distinct().

Distinct in LINQ with anonymous types (in VB.NET)

I can only assume you're dead set on the use of anonymous type as the answer given by Alex Peck is correct. (and I've upvoted it).

However, this boils down to a VB.NET vs C# compiler discussion.

In VB.NET, when an anonymous type is encountered only those properties declared as key properties can be used for comparison purposes. So in VB.NET without key, when you're attempting to do a distinct comparison, nothing will occur.

Read all about it here.

So first, to answer your question, this works with anonymous types:

Dim Countries = From c In List Select New With {Key c.CountryId, c.Country} Distinct.ToList

Sample Image

This is why freedompeace's answer doesn't quite work.

C# however the compiler is a little different.

When an anonymous type is encountered and a comparison operation is needed the c# compiler overrides Equals and GetHashCode. It will iterate over all of the public properties of the anonymous type to compute the object's hash code to test for equality.

And you can read more about that here.

Hope this answers your question.

Linq to Entities Distinct on Column without Anonymous Type

Have you tried using GroupBy?

_UoW.tblcoursRepo.All.GroupBy(c => c.MainHeadingId)
.Select(g => g.FirstOrDefault())

Linq to SQL: DISTINCT with Anonymous Types


dgIPs.DataSource = 
from act in Master.dc.Activities
where act.Session.UID == Master.u.ID
group act by act.Session.IP.Address into g
let ip = g.First().Session.IP
select new
{
Address = ip.Address,
Domain = ip.Domain,
FirstAccess = ip.FirstAccess,
LastAccess = ip.LastAccess,
IsSpider = ip.isSpider,
NumberProblems = ip.NumProblems,
NumberSessions = ip.Sessions.Count()
};

Or:

dgIPs.DataSource = 
from act in Master.dc.Activities
where act.Session.UID == Master.u.ID
group act.Session.IP by act.Session.IP.Address into g
let ip = g.First()
select new
{
Address = ip.Address,
Domain = ip.Domain,
FirstAccess = ip.FirstAccess,
LastAccess = ip.LastAccess,
IsSpider = ip.isSpider,
NumberProblems = ip.NumProblems,
NumberSessions = ip.Sessions.Count()
};

How to LINQ Distinct by Multiple Fields without anonymous types

You can do the following:

return
from c in AllClasses
group c by new
{
c.Batch,
c.Degree_ID,
c.Specialization_ID,
c.CurrentSemester,
c.Section_ID
} into grp
select grp.First();

This takes every first Class from the given group, thus returns a Entity object instead of a anonymous type.



Related Topics



Leave a reply



Submit