我想将连接的设备包括到工作站列表中。但我不仅得到了设备。我也得到了工作站。这是一种循环,没有必要。我怎样才能不再包括工作站,因为这也包括各种其他列表?
//returns to much
var workstations = this.context.TWorkstations
.Include(x => x.TDevices)
.AsQueryable();
//crash -> see error msg
var workstations = this.context.TWorkstations
.Include(x => x.TDevices).ThenInclude(d => d.Select(y => y.Alias))
.AsQueryable();
//crash -> see error msg
var workstations = this.context.TWorkstations
.Include(x => x.TDevices).ThenInclude(d => d.Alias))
.AsQueryable();
错误:
<块引用>表达式 'd.Alias' 在 'Include' 操作中无效, 因为它不代表属性访问:'t => t.MyProperty'。到 在派生类型上声明的目标导航,使用强制转换 ('t => ((Derived)t).MyProperty') 或 'as' 运算符 ('t => (t as 派生).MyProperty')。收藏导航访问可以通过以下方式过滤 组成 Where、OrderBy(Descending)、ThenBy(Descending)、Skip 或 Take 操作。有关包含相关数据的更多信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=746393。
[
{
"id": 102,
"workstation": "workstationName",
"comments": [],
"devices": [
{
"id": 93524,
"alias": "xxx",
"workstation": {
"id": 102,
"workstation": "workstationName",
答案 0 :(得分:1)
Include
旨在返回实体的所有数据,并自动初始化所有已加载的相关属性。所以只需使用完全自定义的投影。
var workstations =
from w this.context.TWorkstations
select new
{
w.Id,
workstation = w.workstationName
devices = w.TDevices.Select(d => d.Alias).ToArray()
};