我正在编写 WCF 服务。我的 sql 查询的输出每次都会根据选择的表返回不同的列数。我将 sql 查询的结果绑定到我的列表。现在我不知道如何将列表与动态返回的列绑定。这是我的服务。
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyService")]
public List<TableList> GetTableListy(Parameters param)
{
try
{
var myReturnList = new List<LookUpSystemTable>();
var passedVersionId = int.Parse((param.VersionId));
var Name = param.Name();
var fromDate = param.FromDate.ToShortDateString();
var toDate = param.ToDate.ToShortDateString();
string selectedColumnNames = "";
var PickupList= from t in _TableEntityRepository.AsQueryable()
where t.Name.Equals(Name)
select t.columns;
var foriegnKeyCol = "k" + Name.Substring(1);
string columnNames = PickupList.FirstOrDefault();
if (columnNames != null){
selectedColumnNames = GetBetween(columnNames.ToUpper(),"SELECT " , " FROM");
selectedColumnNames = selectedColumnNames.Replace("\n", string.Empty);
}
var fromQuery =
"select " + selectedColumnNames + " from " + tableName +
" t Where t.FromDate <= '" + fromDate + "' and t.Id = " +
Id ;
IEnumerable<MyClass> ListOfTables = Context.Database.SqlQuery<MyClass>(fromQuery);
foreach (var t in ListOfTables )
{
// How do i assign dynamically created column returned from sqlquery
}
}
return null;
}
catch (Exception e)
{
throw new WebFaultException<string>(
string.Format(
"{0}{1}",
e.Message, (e.InnerException != null ? e.InnerException.Message : string.Empty)), HttpStatusCode.BadRequest);
}
}
我的 TableList 课程是
public class TableList
{
public int Id { get; set; }
public string Name { get; set; }
}