gpt4 book ai didi

.net - Linq - 从子集合中选择单个项目

转载 作者:行者123 更新时间:2023-12-02 07:43:25 25 4
gpt4 key购买 nike

我有一个父子集合对象,想知道如何使用 Linq 从子集合中获取单个项目

父集合

Public Class FaultCodeModel

Public Property ID As Short
Public Property Description As String
Public Property FaultCodeDetails As List(Of FaultCodeDetailModel)

End Class

child 收藏
Public Class FaultCodeDetailModel

Public Property ID As Short
Public Property Description As String
Public Property NotifyPurchasing As Boolean
Public Property NotifyPurchasingAfterHits As Short
Public Property NotifyExpediting As Boolean
Public Property NotifyExpeditingAfterHits As Short
Public Property NotifyBuyer As Boolean
Public Property NotifyBuyerAfterHits As Short
Public Property NotifySupplier As Boolean
Public Property NotifySupplierAfterHits As Short
Public Property NotiifyProPack As Boolean
Public Property NotiifyProPackAfterHits As Short
Public Property NotifyGoodsInTeamLeader As Boolean
Public Property NotifyGoodsInTeamLeaderAfterHits As Short

End Class

我已经尝试了下面的 Linq 查询,但它返回了匹配父 ID 字段的多个子项。
Dim var = From fcd In FaultCodes Where fcd.FaultCodeDetails.Any(Function(w) w.ID.Equals(faultCodeDetailID))
Select fcd.FaultCodeDetails

如何从子集合中获取单个项目?

最佳答案

我想你想要这个:

FaultCodes.SelectMany(Function(w) w.FaultCodeDetails)
.Where(Function(w) w.ID.Equals(faultCodeDetailID))

这将返回其 ID 的所有子项。等于 faultCodeDetailID .

(我是C#人,可能VB.NET语法有点不对,请自行改正)

C#版本:
FaultCodes.SelectMany(x => x.FaultCodeDetails)
.Where(x => x.ID == faultCodeDetailID)

关于.net - Linq - 从子集合中选择单个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8834521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com