macca18
macca18

Reputation: 197

Using Linq to determine if there is more than 2 distinct items in a class

I have a class that contains a list of another class which has a property that I want to check if it has more than one distinct value.

e.g

public class BasketModel
{
    public BasketModel()
    {
        BasketOrderLines = new List<BasketOrderLine>();
    }
    .
    . 
    .

 public class BasketOrderLine
{
    public int OrderLineId { get; set; }
    public string ImageUrl { get; set; }
    public string ProductType { get; set; }
               .
               .

Given a basket model object I want to find out if there are more than one distinct value in the ProductType.

e.g If all Product Types are "A" then that would be false, if 3 products are of type "A" and one is of type "B" then this would be true.

Cheers Macca

Upvotes: 1

Views: 1163

Answers (5)

Tim Schmelter
Tim Schmelter

Reputation: 460108

Your title: "more than two distinct", your question body: "more than one distinct"

If the title is a typo:

bool notDistinctTypes = theBasket.BasketOrderLine
    .Select(o => o.ProductType)
    .Distinct()
    .Skip(1)
    .Any();

This doesn't need to enumerate all items to find out if there is more than one ProductType.

Upvotes: 3

Lostblue
Lostblue

Reputation: 419

Here is a type extension you can call directly from your list. The pros of this code is to be adaptable to any type implementing IEquals and not only string + kick to use from your code.

The code :

public static class Tools
{
    public static bool fDistinctProductType(this List<BasketOrderLine> lstToAnalyse)
    {
        BasketOrderLine ProductTypeA = lstToAnalyse.FirstOrDefault();
        if (ProductTypeA == null) // It's null when lstToAnalyse is empty
            return false;

        BasketOrderLine ProductTypeB = lstToAnalyse.Where(b => b.ProductType.Equals(ProductTypeA.ProductType)).FirstOrDefault();
        if (ProductTypeB == null) // It's null when it doesn't exists a distinct ProductType
            return false;

        return true;
    }
}

How to call:

List<BasketOrderLine> lst = new List<BasketOrderLine>();
// Add element to list

if (lst.fDistinctProductType())
{
    // DO Something
}

Upvotes: 0

pcdev
pcdev

Reputation: 3052

There are a few ways to do this, here's one:

public class BasketModel
{
    public BasketModel()
    {
        BasketOrderLines = new List<BasketOrderLine>();
    }

    public bool HasMulitpleDistinctProducts
    {
        get
        {
            if (!BasketOrderLines.Any())
            {
                return true; // or false?
            }
            return BasketOrderLines.Select(b => b.ProductType).Distinct().Count() > 1;
        }
    }
}

Upvotes: 0

Antoine Thiry
Antoine Thiry

Reputation: 2442

Something like this :

Public bool CheckDistinct (){
    var res = basketOrderLines.Select(o => o.ProductType).Distinct ().Count ();

    return res > 1;
}

Upvotes: 0

user1023602
user1023602

Reputation:

// Does this basket contains three or more types
public bool HasSeveralTypes(BasketModel basket)
{
    if (basket == null)
        return false;

    int differentTypes = basket.BasketOrderLines 
                               .Select(l => l.ProductType)
                               .Distinct()
                               .Count();

    return (differentTypes > 2);
}

Upvotes: 1

Related Questions