微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用linq在EFcore中展平IEnumerable

如何解决使用linq在EFcore中展平IEnumerable

我正在尝试在EF Core中展平IEnumerable,但是在SelectMany上出现错误

public async Task<IEnumerable<WellDataTableviewmodel>> GetWellsForDataTable() =>
            await context.Wells
            .Include(x => x.Pad)
            .ThenInclude(x => x.Tract)
            .Include(x => x.WellOperations)
            .ThenInclude(x => x.Lessee)
            .Select(p => new WellDataTableviewmodel
             {
                WellId = p.Id,ApiNum = p.ApiNum,WellNum = p.WellNum,PadName = p.Pad.PadName,TractNum = p.Pad.Tract.TractNum,LesseeName = p.WellOperations.SelectMany(x => x.Lessee.LesseeName)
            })
            .ToListAsync().ConfigureAwait(false);

这是错误

错误CS0266
无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'string'。存在显式转换(您是否缺少演员表?)
矿物C:\ tfsmappings \ minerals-development \ minerals \ Repositories \ WellRepository.cs 33有效

这是我尝试填充的viewmodel类:

public class WellDataTableviewmodel
{
        public long WellId { get; set; }
        public string WellNum { get; set; }
        public string ApiNum { get; set; }
        public string PadName { get; set; }
        public string TractNum { get; set; }
        public string LesseeName { get; set; }
}

WellOperations是井上的IEnumerable,每个井操作都有一个承租人,持有他lesseeName类型的字符串

在我的油井课中

public IEnumerable<WellOperation> WellOperations { get; set; }

在井井作业中,我有一个承租人,其中包含lesseeName

public class WellOperation
{
    public long Id { get; set; }
    public Single? OpShare { get; set; }
    public bool? ReportsTotal { get; set; }
    public string Notes { get; set; }
    public long? LesseeId { get; set; }
    public Lessee Lessee { get; set; }
    public long? WellId { get; set; }
    public Well Well { get; set; }
}

解决方法

首先,不要使用group by,如果您通过Select进行了完整的自定义投影,则它们将不执行任何操作,而是使LINQ查询解析变得复杂:

Include(s)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。