我正在尝试从 WCF 数据服务 Web 服务返回数据集。我遇到的问题是直到运行时我才知道类型。
我遵循了此处概述的方法:http: //blogs.msdn.com/b/vitek/archive/2010/11/09/adding-multi-value-properties-to-untyped-providers.aspx
我基本上处于可以从调用中从 DataTable 返回数据的位置。问题是我不能一次通话。我使用以下代码来构建我的输出:
protected override DSPContext CreateDataSource()
{
DSPContext context = new DSPContext();
ResourceSet viewsSet, viewRowsSet, viewRowValuesSet;
this.Metadata.TryResolveResourceSet("Views", out viewsSet);
this.Metadata.TryResolveResourceSet("ViewRows", out viewRowsSet);
this.Metadata.TryResolveResourceSet("ViewRowValues", out viewRowValuesSet);
IList<DSPResource> views = context.GetResourceSetEntities(viewsSet.Name);
IList<DSPResource> viewRows = context.GetResourceSetEntities(viewRowsSet.Name);
IList<DSPResource> viewRowValues = context.GetResourceSetEntities(viewRowValuesSet.Name);
int viewID = 71;
DataTable dtData = GetDataForView(viewID);
var viewObj = new DSPResource(viewsSet.ResourceType);
viewObj.SetValue("ViewID", viewID);
viewObj.SetValue("ViewRows", new List<DSPResource>());
views.Add(viewObj);
int idx = 0;
foreach (DataRow dr in dtData.Rows)
{
var viewRowObj = new DSPResource(viewRowsSet.ResourceType);
viewRowObj.SetValue("RowID", idx);
viewRowObj.SetValue("ViewRowValues", new List<DSPResource>());
//viewRows.Add(viewRowObj);
int innerIDX = 0;
foreach (object colVal in dr.ItemArray)
{
var viewRowValue = new DSPResource(viewRowValuesSet.ResourceType);
viewRowValue.SetValue("FieldName", dtData.Columns[innerIDX].ColumnName);
viewRowValue.SetValue("FieldValue", colVal.ToString());
((List<DSPResource>)viewRowObj.GetValue("ViewRowValues")).Add(viewRowValue);
innerIDX++;
}
((List<DSPResource>)viewObj.GetValue("ViewRows")).Add(viewRowObj);
idx++;
}
return context;
}
protected override DSPMetadata CreateDSPMetadata()
{
DSPMetadata metadata = new DSPMetadata("LogixWCFService", "LogixWCF");
ResourceType view = metadata.AddEntityType("View");
metadata.AddKeyProperty(view, "ViewID", typeof(int));
ResourceType viewRow = metadata.AddEntityType("ViewRow");
metadata.AddKeyProperty(viewRow, "RowID", typeof(int));
ResourceType viewRowValue = metadata.AddEntityType("ViewRowValue");
metadata.AddKeyProperty(viewRowValue, "FieldName", typeof(string));
metadata.AddPrimitiveProperty(viewRowValue, "FieldValue", typeof(string));
ResourceSet views = metadata.AddResourceSet("Views", view);
ResourceSet viewRows = metadata.AddResourceSet("ViewRows", viewRow);
ResourceSet viewRowValues = metadata.AddResourceSet("ViewRowValues", viewRowValue);
metadata.AddResourceReferenceProperty(viewRow, "View", views);
metadata.AddResourceSetReferenceProperty(view, "ViewRows", viewRows);
metadata.AddResourceReferenceProperty(viewRowValue, "ViewRow", viewRows);
metadata.AddResourceSetReferenceProperty(viewRow, "ViewRowValues", viewRowValues);
return metadata;
}
当我尝试 URL“http://localhost/Test.svc/Views()/ViewRows”时,我得到一个行列表,每一行都有一个 ID 和该行中的字段值集合。所有 web 服务返回的是 RowID 列表。为了获取每一行的集合,我需要为每一行调用以下 URL“http://localhost/Test.svc/Views()/ViewRows”。
我希望能够一次性返回所有数据,而不是进行多个服务器调用。
任何人都可以帮助我如何做到这一点?