Distinct() With Lambda

Distinct() with lambda?


IEnumerable<Customer> filteredList = originalList
.GroupBy(customer => customer.CustomerId)
.Select(group => group.First());

Lambda Distinct Select

If you don't override Equals and GetHashCode in your class or provide a custom equality comparer Distinct method uses default equality comparer for the type.And it compares reference types by references. Not by the property values. If you don't want this behaviour either override the relevant methods in your class or if you can't change the class implement an IEqualityComparer for the type and pass it to Distinct:

var ResourceTypeNameList = Resources
.Select(r => new bl_SelectBox{ text=r.ResourceTypeName, value=r.resourceTypeID })
.Distinct(new MyEqualityComparer());

Another quick solution would be using GroupBy:

var ResourceTypeNameList = Resources
.Select(r => new bl_SelectBox{ text=r.ResourceTypeName, value=r.resourceTypeID })
.GroupBy(x => x.SomeProperty)
.Select(x => x.First());

Select all distinct values of variable in object array using lambda expression


var productIds= products.Select(p => p.ProductId).Distict();

But it may be even better to do this directly on the database, as part of the "query" sql command.

Lambda expression to return one result for each distinct value in list

If i understand your question you can try:

var list = callList.GroupBy(x => x.ApplicationID).Select(x => x.First()).ToList();

So if you have a list like:

AppID:1, AppID:1, AppID:2, AppID:2, AppID:3, AppID:3

Will return:

AppID:1 AppID:2 AppID:3

How to use a lambda expression in Enumerable.Distinct()

You can but you need to create a class that implements IEqualityComparer. Then you use Enumerable.Distinct Method (IEnumerable, IEqualityComparer)

How to get distinct instance from a list by Lambda or LINQ

Both Marc's and dahlbyk's answers seem to work very well. I have a much simpler solution though. Instead of using Distinct, you can use GroupBy. It goes like this:

var listDistinct
= list.GroupBy(
i => i.value1,
(key, group) => group.First()
).ToArray();

Notice that I've passed two functions to the GroupBy(). The first is a key selector. The second gets only one item from each group. From your question, I assumed First() was the right one. You can write a different one, if you want to. You can try Last() to see what I mean.

I ran a test with the following input:

var list = new [] {
new { value1 = "ABC", objT = 0 },
new { value1 = "ABC", objT = 1 },
new { value1 = "123", objT = 2 },
new { value1 = "123", objT = 3 },
new { value1 = "FOO", objT = 4 },
new { value1 = "BAR", objT = 5 },
new { value1 = "BAR", objT = 6 },
new { value1 = "BAR", objT = 7 },
new { value1 = "UGH", objT = 8 },
};

The result was:

//{ value1 = ABC, objT = 0 }
//{ value1 = 123, objT = 2 }
//{ value1 = FOO, objT = 4 }
//{ value1 = BAR, objT = 5 }
//{ value1 = UGH, objT = 8 }

I haven't tested it for performance. I believe that this solution is probably a little bit slower than one that uses Distinct. Despite this disadvantage, there are two great advantages: simplicity and flexibility. Usually, it's better to favor simplicity over optimization, but it really depends on the problem you're trying to solve.

Distinct values from a nested list using lambda

I suspect you just want a combination of [SelectMany]1 (to flatten the document list to a document group sequence) and then Distinct to get the distinct elements:

var documentGroups = documentList.SelectMany(d => d.DocumentGroups)
.Distinct()
.ToList();

You'll need to override GetHashCode in DocumentGroup as well, as noted by Servy. In this case that's trivial:

public override int GetHashCode()
{
return DocumentGroupId;
}

Also note that having DocumentGroupId mutable while being the key for equality is worrying. If you can, change it to be immutable and pass the ID into the constructor.

Distinct Count in lambda expression giving one extra count

If the problem is the case then you can use Distinct with StringComparer.CurrentCultureIgnoreCase:

int NewDivision = dt
.AsEnumerable()
.Select(i => i.Field<string>("ParentName"))
.Distinct(StringComparer.CurrentCultureIgnoreCase)
.Count();

Get Distinct Parent Items using Lambda

You can use:

var allCityCountries = CityObjectList.Select(c => c.Region.Country).ToList();

This list is not distinct. To make the countries unique you could either override Equals + GetHashCode in Country, implement a custom IEqualityComparer<Country> for Enumerable.Disinct or use GroupBy(slowest but easiest option):

var distinctCountries = CityObjectList
.Select(c => c.Region.Country)
.GroupBy(c => c.CountryCode)
.Select(g => g.First())
.ToList();

The IEqualityComparer<T> way:

class CountryCodeComparer : IEqualityComparer<Country>
{
public bool Equals(Country x, Country y)
{
if(object.ReferenceEquals(x, y)) return true;
if(x == null || y == null) return false;
return x.CountryCode == y.CountryCode;
}

public int GetHashCode(Country obj)
{
return obj == null ? 0 : obj.CountryCode == null ? 0 : obj.CountryCode.GetHashCode();
}
}

Now you can use Distinct with an instance of it:

var comparer = new CountryCodeComparer();
distinctCountries = CityObjectList.Select(c => c.Region.Country).Distinct(comparer).ToList();

LINQ Select Distinct Count in Lambda form

Use this:

items.Select(i => i.Value).Distinct().Count()


Related Topics



Leave a reply



Submit