0

I have the following classes

public class Group 
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<GroupTier> Tiers { get; set; }
}

public class GroupTier : IEntity
{
    public int Id { get; set; }
    public int GroupId { get; set; }
    public int Tier { get; set; }
    public decimal Amount { get; set; }

    public virtual Group Group { get; set; }
}  

I am trying to map to the following ViewModel

public class GroupViewModel 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<decimal> Tiers { get; set; }
}  

using the configuration

configuration.CreateMap<Group, GroupViewModel>()
   .ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));

I am using EF6 to query from the database. I am having trouble when the Group.Tiers is null. How can I handle the null value?

When I use the this configuration

configuration.CreateMap<Group, GroupViewModel>()
    .ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers == null ? new List<decimal>() : u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));

I am getting this error

Cannot compare elements of type 'System.Collections.Generic.ICollection'

Saanch
  • 1,814
  • 1
  • 24
  • 38

0 Answers0