C#:选择ParentId和Child Text值

本文关键字:Child Text ParentId 选择 | 更新日期: 2023-09-27 17:59:27

我有一个看起来像这样的业务对象模型:

Parent
   string ParentType
   List<Child> ChildCollectionList
Child
   int ChildId
   string ChildText

我的目标是列出具有此签名的元组

Tuple<ParentType,List<string>>  resultingTuple

其中List由ChildCollectionList集合中的ChildText组成。

现在,我有这样的东西。

context.TruckSet.Where(n => n.IsDeleted.Equals(false))
   .Select(p => 
     new Tuple<string,List<string>>      
         (p.TruckType,p.TruckAttributes.Select(n => n.AttrText)
                    .ToList())).ToList();

不用说,Linq查询不喜欢我对TruckAttributes集合的小扩展。

我不确定该怎么做。

C#:选择ParentId和Child Text值

您已经实现的示例有点令人困惑,因为它与上面描述的模型不匹配。但考虑到你只想从你的模型中创建Tuple列表,以下可能会有所帮助:

IList<Tuple<string, IList<string>>> result = 
  parents.Select(p => 
    Tuple.Create(p.ParentType, p.ChildCollectionList.Select(c => c.ChildText).ToList()));

您需要执行以下linq:

List<string> result = (from childItem in yourParentName.ChildCollectionList
             select childItem.childText).ToList();

现在,当你有文本列表时,你可以写以下内容:

Tuple<ParentType,List<string>> resultingTuple = 
            new Tuple<ParentType,List<string>>(yourParentName,result)