2

I am writing WCF service. The output of my sql query will return me different no of columns everytime based on table selected. I bind the result of sql query to my list. Now i dont know how do i bind list with dynamically returned columns. Here is my service.

 [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);
        }


    }

My class for TableList is

public class TableList 
{
     public int Id { get; set; }
     public string Name { get; set; }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
DevelopmentIsMyPassion
  • 3,541
  • 4
  • 34
  • 60

1 Answers1

1

Did you explore DataTable possibility? It allows dynamic columns.

Or use anonymous objects with dynamic properties via SQL LINQ.

Dynamic query data from WCF Data Service to Silverlight Application?

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71