1

I need some help... I have an Entity with an ICollection nested, when I need to make a group by of Work (AdjudicationHead) and AdjYearFFI I cannot set AdjYearFFI as property to group.

Entities:

 public class AdjudicationHead : BaseEntity
 {
     public virtual int Work { get; set; }
     public virtual int AdjYear { get; set; }
     public virtual string AdjType { get; set; }
     public virtual long AdjNumber { get; set; }
     //Navigation
     public virtual ICollection<AdjudicationDetail> AdjudicationDetails { get; set; }
 }

public class AdjudicationDetail : BaseEntity
{
    public virtual int AdjYearFFI { get; set; }

    //FK
     public virtual int AdjYear { get; set; }
     public virtual string AdjType { get; set; }
     public virtual long AdjNumber { get; set; }
    //Navigation
    public virtual AdjudicationHead AdjudicationHead { get; set; }
}

This is what i tried:

public object GetDataCurvaInversionActual()
{
    var query = UoW.GetRepository<AdjudicationHead>()
    .GroupBy(q => new { q.Work, q.AdjudicationDetails.AdjYearFFI });
    var entities = query.ToList();
    var result = Mapper.Map<IList<AdjudicationHead>, IList<Dtos.AdjudicationHead>>(entities);
    return result;
}

Error:

'System.Collections.Generic.ICollection does not contain a definition for AdjYearFFI'

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
  • The `AdjudicationDetails` is a property for a collection of items of type `AdjudicationDetail`. So there isn't any property called `AdjYearFFI` defined on this collection. – Christos Nov 01 '17 at 18:47
  • are you sure you have included the AdjudicationDetails collection in your linq query. The ICollection i.e. AdjudicationDetails in your case is lazily loaded by linq. You have to include it in your qurey something like this context.AdjudicationHead.Includ(a => a.AdjudicationDetails).ToList() – Muhammad Bashir Nov 01 '17 at 18:50
  • You are trying to group on a composite key consisting of a property of the parent class and a key in a child collection with a one to many relationship. You need to choose the level - if its the parent you need to use an aggregate like First or Max on the child, and if you want to group onthe child level, you'll likely need to flatten the parent to repeat it in each child before the group by. – StuartLC Nov 01 '17 at 18:54

1 Answers1

1

The problem here is that an ICollection<AdjudicationDetail> is a collection of AdjudicationDetail, not a AdjudicationDetail (as commented), and so it doesn't have a AdjYearFFI property. It would have only the properties of an ICollection type.

If you want to group AdjudicationDetail's, you can use this query:

var query = UoW.GetRepository<AdjudicationDetail>()
.GroupBy(d => new { d.AdjudicationHead.Work, d.AdjYearFFI });

Now, in your query you have a keyed collection, where your key have an anonymous type with a Work and a AdjYearFFI fields, and the element have a AdjudicationDetail object.