.net core 如何使用Ienumerable返回方法
您好,我有这样的domainmodel:.net core 如何使用Ienumerable返回方法,.net-core,.net Core,您好,我有这样的domainmodel: public string ComponentTitle { get; private set; } public string componentLatinTitle { get; private set; } Severity Code Description Project File Line Suppression State Error CS1061 'Task<IEnumerable<
public string ComponentTitle { get; private set; }
public string componentLatinTitle { get; private set; }
Severity Code Description Project File Line Suppression State
Error CS1061 'Task<IEnumerable<StaticComponent>>' does not contain a definition for 'ComponentTitle' and no accessible extension method 'ComponentTitle' accepting a first argument of type 'Task<IEnumerable<StaticComponent>>' could be found (are you missing a using directive or an assembly reference?) IM.Application
这是我的dto:
现在我想返回我的dto并在GetMethod中用dto映射域,这是我的控制器:
这是我的服务:
public Task<IEnumerable<StaticComponentDto>> GetComponents()
{
var components = _repository.Query().GetAllAsync();
return new StaticComponentDto()
{
ComponentTitle = components.ComponentTitle;
};
}
我的服务不起作用:
我有这样一个错误:
public string ComponentTitle { get; private set; }
public string componentLatinTitle { get; private set; }
Severity Code Description Project File Line Suppression State
Error CS1061 'Task<IEnumerable<StaticComponent>>' does not contain a definition for 'ComponentTitle' and no accessible extension method 'ComponentTitle' accepting a first argument of type 'Task<IEnumerable<StaticComponent>>' could be found (are you missing a using directive or an assembly reference?) IM.Application
帮助我实现我的服务,或者如果它有实现服务或方法的文档,我很乐意学习。谢谢我想您需要使用linq select功能返回: 返回组件。选择Component=>new StaticComponentDto { ComponentTitle=组件。组件标题 }; 您将从存储库中的components变量中获取所有StaticComponents。当您返回一个新的StaticComponentTo时,您指的是哪个组件?C怎么知道?您需要指定 有多种方法可以映射所有对象,其中之一是: 您可以迭代组件并将其映射到新的IEnumerable 注: 另外,如果您在本例中对异步方法进行异步调用,那么您调用的是异步存储库方法-GetAllAsync,那么您应该将您的服务方法设置为异步。这意味着,在GetComponents签名中添加async关键字: 公共异步任务组件 请注意,在这种情况下,我们需要等待来自存储库的调用 您将从存储库中的Components变量中获取所有组件。当您返回一个新的StaticComponentTo时,您指的是哪个组件?C怎么知道?如果要映射所有组件,需要映射循环中的每个对象。
var components = await _repository.Query().GetAllAsync();
var componentsDto = new List<StaticComponentDto>();
foreach (var component in components)
{
var componentDto = new StaticComponentDto();
componentDto.ComponentTitle = component.ComponentTitle;
componentsDto.Add(componentDto);
}
return componentsDto;