user1989
user1989

Reputation: 217

Getting Distinct count from the list

I am trying to find the number of unique dates in my list.

 IEnumerable<T>


public int CountUniqueDate()
        {
            return _items.DistinctBy(i => i.DateMeasured).Count();
        }

I think, this function will tell me the count of the unique dates in the list.

Now, from the other file, I try to retrieve the value

Either I want the count or the condition which will tell whether it is one or not.

So I try this to check whether the count is 1 or not.

But, here i get compilation error. If I do var ans, then I get a full list.

List<T> li;

bool ans= li.Where(x => x.Measures.CountUniqueDate() == 1);

Can anyone please help me.

I wanna check, if the CountUniqueDate returns value 1 or not.

Upvotes: 2

Views: 165

Answers (3)

Omar.Alani
Omar.Alani

Reputation: 4130

Change your method to be an extension method like this

public static class Extensions
{
    public static int CountUniqueDate(this List<Measure> items)   //<= measure is your T type
    {
        return items.DistinctBy(i => i.DateMeasured).Count();
    }
}

then you can call it like this:

List<Measure> li = YourData;

bool ans= li.CountUniqueDate() == 1;

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 505

If you wan't to check if the there is one with count = 1 or not you might use

bool ans= li.Any(x => x.Measures.CountUniqueDate() == 1);

Upvotes: 1

gh9
gh9

Reputation: 10693

The WHERE CLAUSE doesnt return a boolean but an IEnumerable(Of TSource)

Upvotes: 2

Related Questions