我想在工作站列表中包括连接的设备。但我不仅得到了设备。我也去了工作站。这是一个循环,没有必要。我如何才能停止再次包括工作站,因为这也包括了各种其他列表?
//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'.To在派生类型上声明的目标导航,请使用强制转换(not=>((派生(t(.MyProperty'(或"as"运算符(不=>(t as派生的(.MyProperty'(。集合导航访问可以通过筛选撰写Where、OrderBy(Descending(、ThenBy(Descenting(、Skip或Take操作。有关包含相关数据的更多信息,请参阅http://go.microsoft.com/fwlink/?LinkID=746393.
[
{
"id": 102,
"workstation": "workstationName",
"comments": [],
"devices": [
{
"id": 93524,
"alias": "xxx",
"workstation": {
"id": 102,
"workstation": "workstationName",
Include
被设计为返回实体的所有数据,它还自动初始化已经加载的所有相关属性。因此,只需使用完全自定义的投影即可。
var workstations =
from w this.context.TWorkstations
select new
{
w.Id,
workstation = w.workstationName
devices = w.TDevices.Select(d => d.Alias).ToArray()
};