如何将模型对象转换为iEnumerable
我有以下查询,它获取有关容器的数据,如下所示,
public async Task<ExcelListModel>HandleAsync(VesselCrewQuery query)
{
var test= await _queryDispatcher.DispatchAsync<FetchByIdQuery, VesselModel>(new FetchByIdQuery() { Id=query.vesselId});
eModel = await generateExcel(test, lineItems,hint);
}
我想将上述测试变量传递到generateExcel 文件中。
public async Task<ExcelListModel> generateExcel(dynamic header,dynamic lines, string[][] fieldHints = null)
{
var HeaderlistModContent = new List<Dictionary<string, string>>();
// when i get here (IEnumerable) header; it throws an error
var Headerlist =(IEnumerable) header;
foreach (var item in Headerlist)
{
var HeaderModContent = new Dictionary<string, string>();
var HeaderProperty = item.GetType().GetProperties();
foreach (var prop in HeaderProperty)
{
var name = prop.Name.ToString();
var value = GetPropertyValue(item, name).ToString();
HeaderModContent.Add(name, value);
}
HeaderlistModContent.Add(HeaderModContent);
}
}
public static object GetPropertyValue(object source, string propertyName)
{
PropertyInfo property = source.GetType().GetProperty(propertyName);
return property.GetValue(source, null);
}
我想传递测试变量,以便我可以提取上面的标题。但是我收到以下错误 **无法将“VesselModel”类型的对象转换为“System.Collections.IEnumerable”类型。**< /code>
我尝试按如下方式更改它
var test= await _queryDispatcher.DispatchAsync<FetchByIdQuery,List< VesselModel>>(new FetchByIdQuery() { Id=query.vesselId});
,但仍然遇到相同的错误
i have the following query that gets data regarding a vessel as follows
public async Task<ExcelListModel>HandleAsync(VesselCrewQuery query)
{
var test= await _queryDispatcher.DispatchAsync<FetchByIdQuery, VesselModel>(new FetchByIdQuery() { Id=query.vesselId});
eModel = await generateExcel(test, lineItems,hint);
}
i want to pass the above test variable into generateExcel file.
public async Task<ExcelListModel> generateExcel(dynamic header,dynamic lines, string[][] fieldHints = null)
{
var HeaderlistModContent = new List<Dictionary<string, string>>();
// when i get here (IEnumerable) header; it throws an error
var Headerlist =(IEnumerable) header;
foreach (var item in Headerlist)
{
var HeaderModContent = new Dictionary<string, string>();
var HeaderProperty = item.GetType().GetProperties();
foreach (var prop in HeaderProperty)
{
var name = prop.Name.ToString();
var value = GetPropertyValue(item, name).ToString();
HeaderModContent.Add(name, value);
}
HeaderlistModContent.Add(HeaderModContent);
}
}
public static object GetPropertyValue(object source, string propertyName)
{
PropertyInfo property = source.GetType().GetProperty(propertyName);
return property.GetValue(source, null);
}
i want to pass the test variable so that i can extract the headings as above.But i get the following error **Unable to cast object of type 'VesselModel' to type 'System.Collections.IEnumerable'.**
i tried to change it as follows
var test= await _queryDispatcher.DispatchAsync<FetchByIdQuery,List< VesselModel>>(new FetchByIdQuery() { Id=query.vesselId});
but i still get the same error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据签名,
DispatchAsync
方法的返回类型是VeselModel
:它暗示
VesselModel
可以是单个实例(而不是集合)。它甚至由FetchByIdQuery
指示;通常,您应该通过 ID 获取单个项目。甚至错误消息也暗示它不是一个集合。因此,您将单个实例作为参数
动态标头
传递给generateExcel
。并且单个实例无法转换为IEnumerable
。By signature the return type of the method
DispatchAsync
isVeselModel
:It hints that
VesselModel
could be a single instance (not a collection). It's even indicated byFetchByIdQuery
; usually, you should get a single item by Id. Even error message hints that it's not a collection.So, you are passing a single instance to
generateExcel
as the parameterdynamic header
. And a single instance can't be cast toIEnumerable
.