none
dynamically ExecuteStoreQuery RRS feed

  • Question

  • Hi

    I using entity framework, in my Web UI i need to run the query and stored procedure dynamically. I try to use the ctx.ExecuteStoreQuery<T>.  When i give TElement correctly it retun the result correctly. In my case i need dynamically. How i handle this.

    Saturday, October 20, 2012 6:16 AM

Answers

  • Hi ArunPrakash82;

    You could create a generic method and call it with the correct parameters. For example 

    // Generic method
    static public List<T> GetResults<T>(T element, ObjectContext ctx, string cmd) { // List of T elements to be returned. List<T> results = null; // Execute the query results = ctx.ExecuteStoreQuery<T>(cmd, null).ToList(); // Return the results return results; }

    To execute this method you would do something like this.

    // Create the ObjectContext
    var ctx = new MyEntities();
    
    string cmd = @"Select * FROM MyTable1s";
    var resultList = GetResults(new MyTable1(), ctx, cmd);
    
    cmd = @"Select * FROM MyTable2s";
    var resultList2 = GetResults(new MyTable2(), ctx, cmd);
    
    cmd = @"Select * FROM MyTable3s";
    var resultList3 = GetResults(new MyTable3(), ctx, cmd);
    

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Monday, November 5, 2012 9:36 AM
    Sunday, October 21, 2012 12:18 AM

All replies

  • Hi ArunPrakash82;

    You could create a generic method and call it with the correct parameters. For example 

    // Generic method
    static public List<T> GetResults<T>(T element, ObjectContext ctx, string cmd) { // List of T elements to be returned. List<T> results = null; // Execute the query results = ctx.ExecuteStoreQuery<T>(cmd, null).ToList(); // Return the results return results; }

    To execute this method you would do something like this.

    // Create the ObjectContext
    var ctx = new MyEntities();
    
    string cmd = @"Select * FROM MyTable1s";
    var resultList = GetResults(new MyTable1(), ctx, cmd);
    
    cmd = @"Select * FROM MyTable2s";
    var resultList2 = GetResults(new MyTable2(), ctx, cmd);
    
    cmd = @"Select * FROM MyTable3s";
    var resultList3 = GetResults(new MyTable3(), ctx, cmd);
    

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Monday, November 5, 2012 9:36 AM
    Sunday, October 21, 2012 12:18 AM
  • Hi Fernando,

    The generic method is okay. But in my situation i didn't know what table i used in the UI layer. 

    Wednesday, October 24, 2012 10:42 AM
  •   

    When you are about to execute the, ExecuteStoreQuery, you will know what table you will be using, it is in the SQL Select string statement.

      

     

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, October 24, 2012 3:58 PM
  • It doesn't return proper type unless I indicate explicitly as T. If I call GetResult<MyTable1>(new MyTable(), ctx, cmd) it return me result type MyTable1. If I call GetResult(new MyTable(), ctx, cmd) it returns object and I cannot do anything after that. The question is: how can I pass variable of type Type as T?
    Tuesday, April 25, 2017 10:33 PM