2
IEnumerable<IGrouping<StatusType, Request>> group = requests.GroupBy(r=> r.StatusType );

The grouping function above works with when requests (List<Requests>) is from EntityFramework/db.
When changing the assignment of requests from db direct, to a web service,
the grouping isn't working as intended.

Digging a bit, I found that the hash or equality of the StatusType's is different when coming from db vs web (found out thru this post).

From the accepted answer of the post, I can bypass/(resolve?) the problem by overriding..

public class StatusType : IEquatable<int>
{   // omitted other crucial equality comparison components.
    // but for brevity..
    public override int GetHashCode()
    {
        return Id;
    }
}

Although overriding StatusType somewhat resolves the issue,
I feel its quite risky as

  1. I am not the author of the code base.
  2. There are multiple references to StatusType increasing the potential
    of impending failure.

My question,

Is there a way to group by the StatusTypeId (int)

requests.groupBy(r=> r.StatusTypeId) // returns IEnumerable<IGrouping<int,Rquest>>

but get the StatusType?

IEnumerable<IGrouping<StatusType,Rquest>>
Backs
  • 24,430
  • 5
  • 58
  • 85
  • What is your original problem? Sounds like you're going down the wrong path with this fix. – Cory Nelson Dec 22 '17 at 03:44
  • @CoryNelson, its grouping not working when porting my model source from direct db retreival to a web based service. –  Dec 22 '17 at 03:52

2 Answers2

5

Define comparer for StatusType:

public class StatusTypeComparer : IEqualityComparer<StatusType>
{
    public bool Equals(StatusType x, StatusType y)
    {
        return x.Id == y.Id;
    }

    public int GetHashCode(StatusType obj)
    {
        return obj.Id.GetHashCode();
    }
}

Pass it to GroupBy method:

IEnumerable<IGrouping<StatusType, Request>> group = 
                    requests.GroupBy(r => r.StatusType, 
                                          new StatusTypeComparer());
Backs
  • 24,430
  • 5
  • 58
  • 85
0

Disclaimer: Backs has a much better answer than mine but I thought I'd post it anyway in the interests of diversity.

You might be able to get something like the functionality you're looking for by using multiple Linq queries. I don't know if there is an accessible implementation of IGrouping I can use, so I've gone with Tuple<StatusType, List<Request>> instead. It should have a similar effect. So, from your original query:

IEnumerable<IGrouping<int, Request>> group = requests.GroupBy(r=> r.StatusTypeId );

You can add the following line:

IEnumerable<Tuple<StatusType, List<Request>>> groupByStatusType = 
  group.Select(x => new Tuple<StatusType, List<Request>>(x.First().StatusType, 
                                                         x.ToList()));

Or, you can do it all on one line:

IEnumerable<Tuple<StatusType, List<Request>>> group = 
  requests.GroupBy(r => r.StatusTypeId)
          .Select(x => new Tuple<StatusType, List<Request>>(x.First().StatusType, 
                                                            x.ToList()));

You can of course tweak the queries depending on what kind of output you're expecting, but this should at least get you started. Alternately, you could get a similar result by implementing a function that iterates through everything and "manually" creates an output.

Lauraducky
  • 674
  • 11
  • 25