0

I have a DB table that looks similar to this.

ID | Status | Type

etc...etc

I am using linq to try and discern distinct Statuses from this collection like so

results = ctx.Status.Distinct(new StatusComparer()).ToList();

but this returns all statuses, I used the following Link to construct the Comparer below, I found that suggestion in another StackOverflow Post

    public class StatusComparer : IEqualityComparer<Status>
{
    public bool Equals(Status x, Status y)
    {
        // Check whether the compared objects reference the same data. 
        if (ReferenceEquals(x, y))
        {
            return true;
        }

        // Check whether any of the compared objects is null. 
        if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
        {
            return false;
        }

        // Check whether the status' properties are equal. 
        return x.StatusDescription == y.StatusDescription && x.Type == y.Type && x.StatusID == y.StatusID;
    }

    public int GetHashCode(Status status)
    {
        // Get hash code for the Name field if it is not null. 
        var hashStatusId = status.StatusID.GetHashCode();

        // Get hash code for the Code field. 
        var hashStatusDescription = status.StatusDescription.GetHashCode();

        var hashStatusType = status.Type.GetHashCode();

        // Calculate the hash code for the product. 
        return hashStatusId ^ hashStatusDescription ^ hashStatusType;
    }
}
}

My problem is as follows early on we had a system that worked fine, so well in fact they wanted another system using the same Database so we plumbed it in. The search has an advanced options with several filters one of them being Status but as you can see from the above (loose) DB structure statuses have different types but similar text. I need to be able to select via Linq the whole status by the distinct text. all help would be greatly appreciated.

have also tried

results = (from s in context.Status group s by s.StatusDescription into g select        g.First()).ToList();

this also failed with a System.NotSupportedException

Community
  • 1
  • 1
Deviland
  • 3,324
  • 7
  • 32
  • 53

1 Answers1

2

To select all distinct statuses:

ctx.Status.Select(s => new { s.StatusDescription, s.Type }).Distinct();
Thom Smith
  • 13,916
  • 6
  • 45
  • 91
  • 1
    I think the combination of StatusDescription and StatusType is needed to determine the value. – Sam Goldberg Sep 25 '12 at 14:02
  • when I try this I do not get Status as an option from intellisense but I tried anyway and this does not compile, can you explain what you were trying to demonstrate to me? – Deviland Sep 25 '12 at 14:03
  • I'm assuming here that `ctx.Status` is your `IQueryable`. That's what it looks like in the OP. The `Select` and `Distinct` are the LINQ method calls. – Thom Smith Sep 25 '12 at 14:05
  • thanks for the answer but I'm still getting back all statuses :( – Deviland Sep 25 '12 at 14:10
  • Why don't you post the data you're getting and the data you expect to get? – Thom Smith Sep 25 '12 at 14:50