0

I have a table called InvestigatorGroupUser with the following columns: InvestigatorGroupUserId, UserId, and InvestigatorGroupId

I am trying to pull the common InvestigatorGroup that two userid's share but it does not like when I use FirstOrDefault(). I get the message:

"Cannot implicitly convert type System.Linq.IGrouping to InvestigatorGroupUser".

My code is as follows:

    public InvestigatorGroupUser GetCommonGroupId(string userId, string investigatorUserId)
    {
        using (GameDbContext entityContext = new GameDbContext())
        {
            string[] ids = new[] { userId, investigatorUserId };

            return entityContext.InvestigatorGroupUsers
                .Include(i => i.InvestigatorGroup)
                .Where(i => i.InvestigatorGroup.IsTrashed == false)
                .Where(i => ids.Contains(i.UserId))
                .OrderByDescending(i => i.InvestigatorGroupId)
                .GroupBy(e => e.InvestigatorGroupId)
                .Where(i => i.Count() > 1)
                .FirstOrDefault();
                      }
    }

My entity is as follows:

public class InvestigatorGroupUser : IIdentifiableEntity
{
    public InvestigatorGroupUser()
    {
        this.GamesPlayed = new HashSet<GamePlayed>();
        this.AssignedGames = new HashSet<AssignedGame>();
    }

    public int InvestigatorGroupUserId { get; set; }
    public string UserId { get; set; }
    public int InvestigatorGroupId { get; set; }
    public InvestigatorGroup InvestigatorGroup { get; set; }

    public virtual ICollection<GamePlayed> GamesPlayed { get; private set; }
    public virtual ICollection<AssignedGame> AssignedGames { get; set; }

    public int EntityId
    {
        get { return InvestigatorGroupUserId; }
        set { InvestigatorGroupUserId = value; }
    }

}

Thanks for all your help!

Eitan K
  • 837
  • 1
  • 17
  • 39
  • That's because GroupBy returns an IEnumerable>. You need to extract your value from that. – lintmouse Aug 17 '15 at 15:22
  • how would I go about doing that? – Eitan K Aug 18 '15 at 15:25
  • Check this out: http://stackoverflow.com/questions/8521025/how-to-get-values-from-igrouping – lintmouse Aug 18 '15 at 15:48
  • Thanks!. It seemed to work however I was unable to get values from the InvestigatorGroup table. I realized that it might be better to return InvestigatorGroup instead of InvestigatorGroupUser but now my query isn't working properly. I am unable to find contained elements in a navigation table. I've created a new question: https://stackoverflow.com/questions/32079820/select-common-value-in-navigation-table-using-linq-lambda-expression – Eitan K Aug 18 '15 at 18:16

0 Answers0