如何从子列表数据中添加主列表.. [英] How to add in main list from sub list data..

查看:76
本文介绍了如何从子列表数据中添加主列表..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要清单:



列表< object1> LIST1

object1 有属性: Id 姓名



列表< object2> LIST2

object2 包含属性: Id 名称



我想从linq尝试将list2数据添加到list1,以获得包含两个数据的一个平面列表。我试着通过蹩脚的方式得到那个但我想要来自linq ...



我尝试过的事情:



  foreach (item  in  list.selectmenu(a = >  a.list2).toList())
{
// blalballala
}

解决方案

如果 object2 派生自 object1 ,则只能这样做 - 否则你试图将两个不同类型的对象插入相同的清单。

如果相关,你可以很容易地做到这一点:

 class Fruit {} 
class Apple:Fruit { }
class Banana:Fruit {}
cla ss SampleClass
{
public static void Main()
{

List< Fruit> fruits = new List< Fruit>(){new Apple(),new Apple()};
List< Banana> bananas = new List< Banana>(){new Banana(),new Banana()};
fruits.AddRange(香蕉);

}
}

但是如果没有它们之间的某种关系,你只能将它们组合成一个它们都继承的类的List。 / blockquote>

您可以使用Mapper。 Mapper将一个 Type 映射到另一个 Type ,其中属性名称相同,另一个类型必须具有默认构造函数。一个流行的Mapper是 AutoMapper ,这里使用的是一个自制的版本,但语法类似。

列表< Animal> animals =  new  List< Animal> 
{
new Animal(){Name = ,Legs = 4 },
new Animal(){Name = Human,Legs = 2 },
new Animal(){Name = Spider,Legs = 8 }

};
List< Animal2> animals2 = new 列表< Animal2>
{
new Animal2(){Name = Insect,Legs = 6 },
new Animal2(){Name = Fish,Legs = 0 }

};
// source target
Mapper< Animal2,Animal> myMapper = new Mapper< Animal2,Animal>();
var query = animals2.Select(a = > myMapper.Map(a)) ;
animals.AddRange(查询);


I have one main list:

List<object1> LIST1
object1 have properties: Id, Name
and
List<object2> LIST2
object2 with properties: Id, Name

I wanna try from linq to add from list2 data to list1 to have one flat list with both data...I try by lame way to get that but i want from linq...

What I have tried:

foreach (item in list.selectmenu(a => a.list2).toList())
{
//blalballala
}

解决方案

You can only do that if object2 is derived from object1 - otherwise you are trying to insert two different type objects into the same list.
You can do it quite easily if they are related:

class Fruit { }
class Apple : Fruit { }
class Banana : Fruit { }
class SampleClass
    {
    public static void Main()
        {

        List<Fruit> fruits = new List<Fruit>() { new Apple(), new Apple() };
        List<Banana> bananas = new List<Banana>() { new Banana(), new Banana() };
        fruits.AddRange(bananas);

        }
    }

But without some relationship between them, you can only combine them into a List of a class from which they both inherit.


You could use a Mapper. A Mapper maps one Type to another Type where the property names are the same, the other Type must have a default constructor. A popular Mapper is AutoMapper the one used here is a home-brewed version but the syntax is similar.

List<Animal> animals = new List<Animal>
           {
               new Animal(){Name="Dog",Legs=4},
               new Animal(){Name="Human",Legs=2},
               new Animal(){Name="Spider",Legs=8}

           };
           List<Animal2> animals2 = new List<Animal2>
           {
              new Animal2(){Name="Insect",Legs=6},
               new Animal2(){Name="Fish",Legs=0}

           };
           //     source   target
           Mapper<Animal2, Animal> myMapper=new Mapper<Animal2, Animal>();
           var query = animals2.Select(a => myMapper.Map(a));
           animals.AddRange(query);


这篇关于如何从子列表数据中添加主列表..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆