你如何在 Lambda 表达式中进行转换

本文关键字:转换 表达式 Lambda | 更新日期: 2023-09-27 17:56:19

考虑到最后一个是我想要的,我在选择上收到错误:

AwardsListViewModel viewModel = AwardListViewModel
{
     menuChild = repository
                   .menuChild
                   .Where(p => p.MenuParentAcronym == "Awards Processing" 
                               && p.IsActive == "True")
                   .OrderBy(c => c.DisplayOrder)
                   .Select(m => m.Description == "Awards Processing List")
};

错误是:无法隐式将类型 System.Linq.IQueryable<bool> 转换为 System.Collections.Generic.IEnumerable<AwardsSystem30.Domain.Entities.MenuChild> 。 存在显式转换(您是否缺少强制转换?

如何投射???

你如何在 Lambda 表达式中进行转换

我怀疑您的查询没有按照您的预期执行 - 它返回一个IQueryable<bool>。我猜最后一个Select子句应该是一个Where,而不是按预期工作。

如果这是正确的,下面的代码应该可以工作(我已经将 where 位合并为一个)

AwardsListViewModel viewModel = AwardListViewModel
{
     menuChild = repository
                   .menuChild
                   .Where(p => p.MenuParentAcronym == "Awards Processing" 
                               && p.IsActive == "True")
                               && p.Description == "Awards Processing List")
                   .OrderBy(c => c.DisplayOrder)
};

您可以投射到列表。

AwardsListViewModel viewModel = AwardListViewModel
{
     menuChild = repository
                   .menuChild
                   .Where(p => p.MenuParentAcronym == "Awards Processing" 
                               && p.IsActive == "True")
                   .OrderBy(c => c.DisplayOrder)
                   .Select(m => m.Description == "Awards Processing List")
                   .ToList()
};