使用动态返回的列绑定列表

时间:2013-07-02 06:09:40

标签: c# list wcf-rest

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

1 个答案:

答案 0 :(得分:1)

你有没有探索DataTable的可能性?它允许动态列。

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

Dynamic query data from WCF Data Service to Silverlight Application?