2

我正在编写 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; }
}
4

1 回答 1

1

你探索过DataTable可能性吗?它允许动态列。

或者通过 SQL LINQ 使用具有动态属性的匿名对象。

从 WCF 数据服务到 Silverlight 应用程序的动态查询数据?

于 2013-07-02T06:17:33.837 回答