开发者

Add list to a list

开发者 https://www.devze.com 2023-01-02 07:26 出处:网络
I have the following code: var columnNames = (from autoExport in dataContext.AutoExports where autoExport.AutoExportTemplate != null

I have the following code:

var columnNames = (from autoExport in dataContext.AutoExports
               where autoExport.AutoExportTemplate != null
                  && ContainsColumn(autoExport.AutoExportTemplate, realName)
               select GetDbColumnNames(autoExport.AutoExportTemplate, realName)).ToList();

Where the function GetDbColumns() returns an List<string>. So columNames is of the type List<List<string>>. Is it possible to create a List<string>, so each element of the list of GetDbCol开发者_JS百科umns is added to the result of the LinQ query?


You can use the "select many" construction:

var columnNames = (
    from autoExport in dataContext.AutoExports
    where autoExport.AutoExportTemplate != null
          && ContainsColumn(autoExport.AutoExportTemplate, realName)
    from column in GetDbColumnNames(autoExport.AutoExportTemplate, realName)
    select column).ToList();

Or here is an alternative way of using SelectMany:

var columnNames = (
    from autoExport in dataContext.AutoExports
    where autoExport.AutoExportTemplate != null
          && ContainsColumn(autoExport.AutoExportTemplate, realName)
    select autoExport
).SelectMany(x => x.GetDbColumnNames(autoExport.AutoExportTemplate, realName))
.ToList();

And finally, this is another way to put it (but it includes the somewhat ugly code x => x):

var columnNames = (
    from autoExport in dataContext.AutoExports
    where autoExport.AutoExportTemplate != null
          && ContainsColumn(autoExport.AutoExportTemplate, realName)
    select autoExport.GetDbColumnNames(autoExport.AutoExportTemplate, realName)
).SelectMany(x => x).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消