c# - 使用 LinkEntity 从 EntityCollection 获取数据组
问题描述
我有一个使用 QueryExpression 和 LinkEntity (InnerJoin) 检索的扁平数据结构。我用“元素”给孩子起了别名
所以我的数据看起来像这样:
parentIdPK parentStatus element.ChildIdPK element.parentIdFK
1 100 10 1
1 100 11 1
1 100 12 1
2 100 13 2
2 100 14 2
3 100 15 3
3 100 16 3
3 100 17 3
所以基本上我有一个父/子结构,我想将这些数据推送到我自己的类中:
public class ExistingProposal
{
public Guid parentIdPK { get; set; }
public int parentStatus { get; set; }
public List<ExistingElement> Elements { get; } = new List<ExistingElement>();
}
public class ExistingElement
{
public Guid ChildIdPK { get; set; }
public Guid parentIdFK { get; set; }
}
所以一般来说,这将导致一个 ExistingProposal 与 N ExistingGRProposalElement's
我能以最好的方式实现这一目标吗?我已经尝试过使用 linq,但我对此非常挣扎。
我实际上正在尝试使用 linq 对数据进行分组:
var groups = from a in result.Entities
orderby a.Attributes["parentId"]
group a by a.Attributes["parentId"] into g
select new { g };
我实际上遇到的问题是我不知道从哪里开始创建所需的类结构。
也许有人可以指出我正确的方向?
任何提示都非常感谢。
解决方案
您的问题不是很清楚,但是,如果我理解得很好,以下表达式将为您解决问题:
var groups = from a in result.Entities
group a by a.Attributes["parentId"] into g
select new ExistingProposal {
parentIdPK = a.Key,
parentStatus = (int)a.FirstOrDefault().Attributes["parentStatus"],
Elements = (from y in g
select new ExistingElement {
ChildIdPK = y.Attributes["element.ChildIdPK"],
parentIdFK = a.Key
}).ToList()
};
- 您需要在您的
Elements
属性中添加一个 setterExistingProposal
- 分组前无需下单
- 您应该将中间变量(y、g、a 等)重命名为更有意义的变量
推荐阅读
- kotlin - 为什么 kotlin multiplatform 不执行和导出 iOS 框架?
- html - Bootstrap 4 设置高度:100%;到具有 flex-grow:1 的 flex 项的子项
- c# - 如何从另一个泛型方法调用泛型属性
- go - 如何解决“请求的资源上不存在‘Access-Control-Allow-Origin’标头”
- python - 如何使用嵌套的 for 循环来查找线性方程的 x 和 y
- wordpress - 仅使用 wordpress 通过电子邮件自动登录用户
- javascript - 状态更改时自动响应本机打开模式
- scala - 如何从逗号分隔的字符串构建地图行?
- python-2.7 - 如何修复 pip 安装错误:'EnvironmentError: [Errno 42] Illegal byte sequence'?
- android - 所有方向的 4 个填充和只有 1 个填充有什么区别?