2014-06-08 76 views
0

我有丰富的NHibernate DBO对象,我需要查询提取关键字和国家代码相关的阵列。LINQ的创建DTO对象的数组,在一个阵列它

为了解释: 我有3个相关DBO对象从数据库中,具有以下结构返回:

class DboCountry{ 
    public int CountryId {get;set;} 
    public string CountryCode {get;set;} 
}  

class DboPage{ 
    public int Id {get;set;} 
    public string Keyword {get;set;} 
} 

class DboPageCountry{ 
    public int Id {get;set;} 
    public DboThemePage DboThemePage {get;set;} 
    public DboCountry DboCountry {get;set;} 
} 

这样询问我得到包含多个重复DboPage DboPageCountries的目录数据库时。被关联到各个DboCountry.CountryCode关键字

我需要做的是采取DboPageCountries的名单,并创建DtoKeywordCountryCode数组结构如下:

class DtoKeywordCountryCode{ 
    public string Keyword {get; set;} 
    public string[] CountryCodes {get; set;} 
} 

到目前为止,使用LINQ,我要么能组中的关键字,但没有得到相关的国家代码,或者获取关键字和国家代码,但不作为对适用CountryCodes数组的唯一关键字相关联。

在正确的方向的任何指针赞赏。

回答

1

试试这个 -

var items = new List<DboPageCountry>(); //list of DboPageCountries 

var items2 = from x in items.GroupBy(x => x.DboThemePage) 
      select new DtoKeywordCountryCode { Keyword = x.First().DboThemePage.Keyword, CountryCodes = x.Select(c => c.DboCountry.CountryCode).ToArray() }; 
+0

太好了,我还发现了下面的混合查询选项 –

0
DtoThemePage[] dtoThemePageArrayFull = (from tpc in dboThemePageCountryList group tpc by tpc.DboThemePage.Keyword into k 
             select new DtoThemePage 
             { 
              Keyword = k.Key, 
              CountryCodes = k.Select(c => c.DboCountry.CountryCode).ToArray<string>() 
             }).ToArray<DtoThemePage>(); 
相关问题