LinqToEntities GroupJoin ArgumentException

时间:2019-06-12 23:42:33

标签: c# linq linq-to-entities left-join

我有以下模型:

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; }
}

即使UserPageSettings表没有具有此类FK的记录,我也想选择所有UserPages。 我有以下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();

但是出现以下异常:

  

ArgumentException:类型的表达式   'System.Collections.Generic.IEnumerable'1 [TmsApi.Core.UserPageSetting]'   不能用于返回类型   'System.Collections.Generic.IEnumerable'1 [TmsApi.Dto.UserPageSettingDto]'

怎么了?

0 个答案:

没有答案