我期待的最终响应应该是这样的:
[
{
"ID": 4,
"Floor": "BackOffice Floor",
"OverallDescription": "Pretty Pretty Good",
"Permit": "Admin",
"TabOne": [
{
"Color": "Blue",
"status": "ReadWrite"
},
{
"Color": "Purple",
"status": "ReadOnly"
},
],
"TabTwo": [
{
"Color": "Black",
"status": "Offline"
},
{
"Color": "Green",
"status": "Online"
},
{
"Color": "White",
"status": "Blinking"
},
],
}
]
我为class
建模如下:
public class AllSheets
{
// from parent calss
public int ID { get; set; }
public string Floor { get; set; } = string.Empty;
public string OverallDescription { get; set; } = string.Empty;
public string Permit { get; set; } = string.Empty;
// from children classes
public IEnumerable<SheetChildOne> TabOne { get; set; } = Enumerable.Empty<SheetOne>();
public IEnumerable<SheetChildTwo> TabTwo { get; set; } = Enumerable.Empty<SheetTwo>();
}
这里有三个classes
,一个是parent
,另外两个是children
。他们的JOIN
是在ID
领域。
public class SheetMain
{
public int ID { get; set; }
public string Floor { get; set; } = string.Empty;
public string OverallDescription { get; set; } = string.Empty;
public string Permit { get; set; } = string.Empty;
}
public class SheetChildOne
{
public int ID { get; set; }
public string Floor { get; set; } = string.Empty;
public string Color { get; set; } = string.Empty;
public string Status { get; set;} = string.Empty;
}
public class SheetChildTwo
{
public int ID { get; set; }
public string Floor { get; set; } = string.Empty;
public string Color { get; set; } = string.Empty;
public string Status { get; set;} = string.Empty;
}
注意,我不想重复**一些在子节点和父节点中通用的字段,例如ID
和Floor
。
我可以自己开始写,但我有两件事需要帮助:**1:**如何在查询中加入多个group-join,以便将SheetChildTwo也包括在内?**2:**我如何告诉它不包括一些在parent和child中重复的字段,例如本例中的ID和Floor。
以下是我尝试的:
var result =
sheetMain.GroupJoin
(sheetChildOne,
work1 => work1.ID,
work2 => work2.ID,
(t1, t2) => new AllSheets
{
ID = t1.ID,
Floor = t1.Floor,
OverallDescription = t1.OverallDescription,
Permit = t1.Permit,
TabOne = t2.Select
(b => new SheetChildOne
{
Color = b.Color,
Status = b.Status,
}
)
}
);
2条答案
按热度按时间68bkxrlz1#
尝试以下查询。使用查询语法,因为它在这里更可读:
tgabmvqs2#
这感觉就像一个XY problem。我对这个主题的快速实验。
在这两种情况下,控制台显示:
1.匿名对象
1.专用类