How May I extract this ternary operator in independent statement Somehow this code looks messy to me:

 var results = query.GroupBy(x =&gt; x.ProductStatusId == ProductStatus.Sold || x.ProductStatusId == ProductStatus.OnHold ? "Delivered" :
                                x.ProductStatusId == ProductStatus.Shiped ? "Shiped" :
                                x.ProductStatusId == ProductStatus.NotAvailable ? "NotAvailable" :
                                x.ProductStatusId == ProductStatus.Paid ? "Paid" : "New").Where(g =&gt; g.Key != "New")


Is it possible to extract this ternary operator in independent statement.

Thanks!

Cheers
You could do query.GroupBy(MyTernaryWrapperMethod) and declare that method as

string MyTernaryWrapperMethod(xVariableType x)
{
    if(x.ProductStatusId == ProductStatus.Sold || x.ProductStatusId == ProductStatus.OnHold) return "Delivered";

    if(x.ProductStatusId == ProductStatus.Shiped) return "Shiped";

    if(x.ProductStatusId == ProductStatus.NotAvailable) return "NotAvailable";

    if(x.ProductStatusId == ProductStatus.Paid) return "Paid";

    return "New"
}


But, that method seems to return something being an actual property of x. So maybe you should add a new method to x's type. Also it would be better to use swicth/case instead of those ifs:

public string ProductStatusName()
{
    switch (ProductStatusId)
    {
        case ProductStatus.Sold:
        case ProductStatus.OnHold: return "Delivered";
        case ProductStatus.Shiped: return "Shiped";
        case ProductStatus.NotAvailable: return "NotAvailable";
        case ProductStatus.Paid: return "Paid";
        default: return "New"
    }
}


And then you do:

var results = query.GroupBy(x =&gt; x.ProductStatusName()).Where(g =&gt; g.Key != "New")