Linq 列表中的c#Lambda查询,其中包含用于映射的列表

Linq 列表中的c#Lambda查询,其中包含用于映射的列表,linq,lambda,Linq,Lambda,今天我将我的对象映射到DTO,如下所示 public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID) { return _articleRepository.GetArticlesByCategory(catSection, headCategoryID, customerREFID).Sele

今天我将我的对象映射到DTO,如下所示

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository.GetArticlesByCategory(catSection, headCategoryID, customerREFID).Select(a => Mapper.ToDTO(a)).ToList();
}
public IEnumerable GetArticlesBasedOnCategorySection(int-catSection,int-headCategoryID,string-customerREFID)
{
返回_articleRepository.GetArticlesByCategory(catSection,headCategoryID,customerREFID)。选择(a=>Mapper.ToDTO(a)).ToList();
}
但在变量中,我有另一个列表,我想以类似的方式映射它。
是否可以像这样在一行中写入所有内容,或者我必须先编写一个
foreach
循环,然后映射一个.List。

如何在匿名对象中返回文章及其项目

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository
        .GetArticlesByCategory(catSection, headCategoryID, customerREFID)
        .Select(a => new 
                     { 
                         Article = Mapper.ToDTO(a),
                         Items = a.Items.Select(b => Mapper.ToDTO(b)).ToList()
                     })
        .ToList();            
}
public IEnumerable GetArticlesBasedOnCategorySection(int-catSection,int-headCategoryID,string-customerREFID)
{
返回存储库
.GetArticlesByCategory(catSection、headCategoryID、customerREFID)
.选择(a=>新建
{ 
Article=制图员ToDTO(a),
Items=a.Items.Select(b=>Mapper.ToDTO(b)).ToList()
})
.ToList();
}

一种方法是使用具有多条语句的lambda。我不确定这是否可以被视为一行程序,而多语句lambda也不是很LINQ-y

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository
        .GetArticlesByCategory(catSection, headCategoryID, customerREFID)
        .Select(a =>
                {
                    ArticleDTO article = Mapper.ToDTO(a);
                    article.Items = a.Items.Select(b => Mapper.ToDTO(b)).ToList();
                    return article;
                })
        .ToList();
}
public IEnumerable GetArticlesBasedOnCategorySection(int-catSection,int-headCategoryID,string-customerREFID)
{
返回存储库
.GetArticlesByCategory(catSection、headCategoryID、customerREFID)
.选择(a=>
{
ArticleDTO article=Mapper.ToDTO(a);
article.Items=a.Items.Select(b=>Mapper.ToDTO(b)).ToList();
退货物品;
})
.ToList();
}
如果ArticleDTO有一个复制构造函数,您可以这样编写:

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository
        .GetArticlesByCategory(catSection, headCategoryID, customerREFID)
        .Select(a => new ArticleDTO(Mapper.ToDTO(a))
                     {
                         Items = a.Items.Select(b => Mapper.ToDTO(b)).ToList()
                     })
        .ToList();
}
public IEnumerable GetArticlesBasedOnCategorySection(int-catSection,int-headCategoryID,string-customerREFID)
{
返回存储库
.GetArticlesByCategory(catSection、headCategoryID、customerREFID)
.Select(a=>newarticledto(Mapper.ToDTO(a))
{
Items=a.Items.Select(b=>Mapper.ToDTO(b)).ToList()
})
.ToList();
}

您也可以在构造函数或
Mapper.ToDTO(a)

中映射项,抱歉,这不是我想要的。也许在a获得文章列表后,我必须执行foreach循环。@Tan:您希望结果是什么样的?把foreach循环版本编辑到你的问题中怎么样?你的方法产生了一个错误,所以我初始化了一个新的ArticleDTO列表,并在你的匿名列表的帮助下进行了foreach循环来填充它。谢谢@谭:很高兴听到你成功了!然而,我正在考虑删除这个答案,因为它是不正确的,并且仍然需要使用foreach循环。当然,只有在你同意的情况下,我才会移除它。