下载嵌套集合

Imp*_*erC 2 c# asp.net .net-core asp.net-core

如何从带有嵌套集合的上下文中获取对象作为id列表?

我想获得一个包含用户ID列表的Flat对象.

public IEnumerable<Flat> GetAll()
{
    return _context.Flats
        .Include(flat => flat.Users.Select(x => x.Id));
}
Run Code Online (Sandbox Code Playgroud)

错误返回:

The property expression 'flat => {from User x in flat.Users select [x].Id}' is not valid. The expression should represent a property access: 't => t.MyProperty'.

小智 5

这应该是一种方法:

public IEnumerable<dynamic> GetAll()
{
    return _context.Flats
        .Include(flat => flat.Users)
        .Select(flat => new { Flat = flat, UserIds = flat.Users.Select(u => u.Id) });
}
Run Code Online (Sandbox Code Playgroud)

虽然您可能想要定义要返回的新类型.