繁体   English   中英

一次在linq中加入两个列表初始化属性

[英]join two list in linq initializing properties one time

我在linq中找到了许多要加入列表的示例,但是我的确切问题没有解决。

我必须加入列表列表和列表

我已经初始化了List的所有属性,并且在加入时必须再次初始化A的所有属性以及从B添加的属性。

List<A> listB = from c in country
                select new A
                {
                  CountryId=c.CountryId 
                  CountryName=c.CountryName  
                } ;
List<A> listA = from d in data
                select new A
                {
                  Name = d.Name
                  Age= d.Age,
                  City=d.City,
                  CountryId=d.CountryId   
                } ;

现在要初始化列表中的Country属性,我将同时加入两个列表,问题开始了

  listA = from d in listA
            join c in listB on d.CountryId=c.CountryId
                    select new A
                    {
                      Name = d.Name
                      Age= d.Age,
                      City=d.City,   
                      Country=c.CountryName
                    } ;

在上面的连接中看到,我必须再次初始化名称,年龄和城市才能在一个地方初始化属性。

不要使用listAlistB直接加入国家和数据:

from c in country
join d in data
   on c.CountryId equals d.CountryId 
select new A {
      Name = d.Name
      Age = d.Age,
      City = d.City,
      Country = c.CountryName
};

顺便说一句,为什么您都对listAlistB使用A类,如果它们都包含不同的数据? 应该是两个不同的类

暂无
暂无

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

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