0

I have the following model:

public class UserPage
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Path { get; set; }
    public bool Editable { get; set; }
}

public class UserPageSetting
{
    public int UserId { get; set; }
    public int PageId { get; set; }
    public bool Published { get; set; } = true;
    public DateTime? Modified { get; set; }

    public virtual UserPage UserPage { get; set; }
}

I want to select all UserPages, even if UserPageSettings table does not have records with such FK . I have the following linq:

        var joined = _dbContext.UserPages.GroupJoin(_dbContext.UserPageSettings, i => i.Id, o => o.PageId, (i, o) => o
           .Select(x => new UserPageSettingDto { Editable = i.Editable, Id = i.Id, Modified = x.Modified, Name = i.Name, Path = i.Path, Published = x.Published })
           .DefaultIfEmpty(new UserPageSettingDto { Id = i.Id, Name = i.Name, Path = i.Path, Published = true, Editable = true }))
           .SelectMany(x => x).ToList();

but I get the following exception:

ArgumentException: Expression of type 'System.Collections.Generic.IEnumerable'1[TmsApi.Core.UserPageSetting]' cannot be used for return type 'System.Collections.Generic.IEnumerable'1[TmsApi.Dto.UserPageSettingDto]'

what is wrong?

Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • If i understood correctly, you need to add DefaultIfEmpty after your join UserPageSetting then you can select UserPageSettingDto. – Mdyahiya Jun 13 '19 at 01:27
  • 1
    This might be useful link to check https://stackoverflow.com/questions/15595289/linq-to-entities-join-vs-groupjoin – Mdyahiya Jun 13 '19 at 01:38

0 Answers0