简体   繁体   English

linq lambda将具有相同类型的对象列表的对象转换为另一个对象

[英]linq lambda convert object with list of objects of the same type to another object

I currently have a List<OriginalItem> that i need to convert to a List<NewItem> . 我目前有一个List<OriginalItem> ,我需要将其转换为List<NewItem>

Here are the classes 这是课程

public class OriginalItem
{
   public int ItemIndex {get;set;}

   public string Name {get;set;}

   private OriginalItem[] itemsList;

   public OriginalItem[] ItemsList
   {
        get
        {
            return itemsList;
        }
        set
        {
            itemsList= value;
        }
   }

}

public class NewItem
{
  public int NewItemIndex {get;set;}
  public string NewName {get;set;}

  private NewItem[] itemsList;

  public NewItem[] ItemsList
  {
      get
      {
          return itemsList;
      }
      set
      {
         itemsList= value;
      }
   }
}

I know using a select statement i can create a new object from a list ie 我知道使用选择语句,我可以从列表中创建一个新对象,即

List<NewItem> newItems = originalItems.Select(x=> new NewItem(){
                                                            NewItemIndex = x.ItemIndex,
                                                            NewItemName = x.Name
                                                           }).ToList();

but how do i create the list within the list? 但是如何在列表中创建列表? It doesnt have to use recursion, if there is another way to do it. 如果有另一种方法,则不必使用递归。

Thanks, 谢谢,

from the above i managed to get what i need with minor changes: 从上面我设法通过较小的更改得到了我需要的东西:

public class OriginalItem
{
.
.
.
   //add new method to convert the originalItem to newItem
   public NewItem createNewItem()
   {
      NewItem item = new NewItem();
      item.NewName = this.Name;
      item.NewItemIndex = this.ItemIndex;
      item.ItemsList = this.ItemsList.Select(x =>x.createNewItem()).ToList();
   }
}

and then in the main class where i had the List<OriginalItem> originalItems I did the following: 然后在我拥有List<OriginalItem> originalItems的主类中,执行以下操作:

List<NewItem> newItems = originalItems.Select(x=>x.createNewItem()).ToList();

You can't get by without a recursion with recursive properties. 如果没有具有递归属性的递归,就无法实现。 This operation is essentially recursive. 此操作本质上是递归的。

One possible implementation: 一种可能的实现:

NewItem ToNew(OriginalItem item)
{
    return new NewItem() { NewItemIndex = item.ItemIndex, NewName = item.Name, ItemsList = item.ItemsList == null ? null : item.ItemsList.Select(ToNew).ToArray()};
}

And here is how to use it: 这是如何使用它:

var newItems = originalItems.Select(ToNew).ToList();

It also could be implemented as a copy constructor, but no reason to cople the two classes. 也可以将其实现为副本构造函数,但无需增加这两个类的数量。

Also it's more convenient to use as a method group (just ToNew , not i => ToNew(i) ). 同样,将其用作方法组也更方便(只是ToNew ,而不是i => ToNew(i) )。

Here's how I would do it: 这是我的处理方式:

Func<OriginalItem, NewItem> convert = null;
convert = oi =>
    new NewItem()
    {
        NewName = oi.Name,
        NewItemIndex = oi.ItemIndex,
        ItemsList = oi.ItemsList == null
            ? null
            : oi.ItemsList.Select(x => convert(x)).ToArray(),
    };

var newItems = originalItems.Select(oi => convert(oi)).ToList();

So, given this input: 因此,鉴于此输入:

var originalItems = new List<OriginalItem>()
{
    new OriginalItem()
    {
        Name = "0", ItemIndex = 0,
        ItemsList = new []
        {
            new OriginalItem()
            {
                Name = "1", ItemIndex = 1, ItemsList = null,
            },
            new OriginalItem()
            {
                Name = "2", ItemIndex = 2, ItemsList = null,
            },
        },
    },
    new OriginalItem()
    {
        Name = "3", ItemIndex = 3, ItemsList = null,
    },
};

I get this output: 我得到以下输出:

新东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM