【发布时间】:2019-04-10 14:14:03
【问题描述】:
我正在用 c# 编写一个 Web 应用程序,我在其中查阅了 Azure 中的 CosmoDB 中的数据。这个 cosmoDB 包含一些数据结构的 Json 文档,这些数据结构在我的 Web 应用程序中定义为模型。问题是,当查询在数据库中具有不同类型的结构时,我必须将其作为对象类型进行,以便稍后将其转换为特定类型并以 html 代码在屏幕上显示。实际上,我拥有的代码是根据我的应用程序为您提供修改的 Azure 教程的代码。
代码如下:
namespace todo
{
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DocumentDBRepository<Object>.Initialize();
}
}
}
然后 Home 控制器生成:
[ActionName("ECCE")]
public async Task<ActionResult> ECCEAsync(string id)
{
var items = await DocumentDBRepository<Object>.GetItemsAsync(id);
return View((ECCE_SupportData)items);
}
DocumentDBRepository 类在哪里生成:
public static async Task<IEnumerable<T>> GetItemsAsync(string collectionId)
{
List<T> results = new List<T>();
try
{
IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.AsDocumentQuery();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<T>());
}
}
catch (Exception)
{
return results;
}
return results;
}
稍后将对象列表传递给 html 以在屏幕上查看它们:
<script type="text/javascript" language="javascript" src="../../Scripts/TableFilter/tablefilter.js"></script>
@model IEnumerable<todo.Models.ECCE.ECCE_SupportData>
@{
ViewBag.Title = "Data";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ECCE Data</h2>
@if (Model.Count() != 0)
{
<table class="table" id="ECCETable">
<tr>
<th>
@Html.DisplayNameFor(model => model.ECCE_CnfStatus)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_ComsStatus.ComStatus)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_ComsStatus.UltimoError)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_Status.AppVersion.IdApp)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_Status.AppVersion.ReqVersion)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_Status.AppVersion.SwVersion)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_Status.AppStatus)
</th>
<th>
@Html.DisplayNameFor(model => model.ECCE_UvStatus.NumUVs)
</th>
</table>
问题是,在 HomeController 中,当我尝试将对象转换为我的数据结构时,ECCE_SuportData 给了我一个错误:您无法转换类型为 'System.Collections.Generic.List`1 [System.Object ]' 类型为'todo.Models。 ECCE.ECCE_SupportData '
有谁知道如何将对象转换为这种数据结构? 提前致谢。
【问题讨论】:
-
请用英文发帖。如果你不会说英语:Stack Overflow en español.
-
用英文写
-
好的,我现在就写
-
在方法
EcceAsync中,使用类型todo.Models.ECCE.ECCE_SupportData作为泛型参数如下:var items = await DocumentDBRepository<ECCE.ECCE_SupportData>.GetItemsAsync(id); -
如果我这样做,我不会获得任何数据,因为我将 DocumentDBRepository 初始化为
标签: c# azure web azure-cosmosdb