5

I have situation where a storeprocdure return collection, but I do not how the object structure because the query is very dynamic.

One query can return:

Id | Location | MarketSegment | ... n columns

and another can return

Id | Sales Rep | Location | Region | ... n columns

I am simply just return a "object" as you can see in the code below. I know this won't work, but how can I set it up so it does?

using (DbContext db = new Context())
{

    var items = db.Database.SqlQuery<object>(
        "SP @Param1, @Param2",
        new SqlParameter("Param1", ped),
        new SqlParameter("Param2", 25)
    ).ToList();

    return Request.CreateResponse<List<object>>(HttpStatusCode.OK, items);
}

EDIT:

I don't know if showing the SP will help in anyways, except if I can explain it more.

Each columns are represented as Custom Fields. Users are able to create n numbers of Custom Fields. So If you run the SP for User1 and he has 5 custom fields, then each custom fields will be represented in Columns, but If User2 has 3 custom fields, only 3 columns will be represented. What I don't have control over is the Custom Field Name and number of custom fields.

2
  • Please show us what SP is. Commented Aug 25, 2014 at 21:14
  • please see edited comment. Commented Aug 25, 2014 at 21:31

3 Answers 3

10

You can't use SqlQuery<T> for custom fields.

Creates a raw SQL query that will return elements of the given generic type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. - MSDN

But, you can use ExecuteReader to achieve that.

using (var db = new Context())
{
    db.Database.Connection.Open();

    var cmd = db.Database.Connection.CreateCommand();
    cmd.CommandText = "SP @Param1, @Param2";
    cmd.Parameters.Add(new SqlParameter("Param1", ped));
    cmd.Parameters.Add(new SqlParameter("Param2", 25));

    List<List<object>> items = new List<List<object>>();
    var reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        var item = new List<Object>();
        items.Add(item);

        for (int i = 0; i < reader.FieldCount ; i++)
            item.Add(reader[i]);
    }

    return Request.CreateResponse<List<object>>(HttpStatusCode.OK, items);
}
Sign up to request clarification or add additional context in comments.

Comments

10

If on SQL 2016 or newer, add "FOR JSON AUTO" to your query to return as JSON, e.g:

var json = db.Database.SqlQuery<string>("Select x, y, z FROM tbl FOR JSON AUTO").First();

Then use Json.Net to create a dynamic object using

var myDynamic = JObject.Parse(json)

1 Comment

It might give error in ef core 8. Use the below one db.Database.SqlQuery<string>("Select x, y, z FROM tbl FOR JSON AUTO").ToList().First();
-3

If you know what all the possible columns could be returned, there is no issue with using a class that has more properties than you need.

public class Data
{
public int ID {get;set;}
public string SalesRep {get;set;}//Simply will be empty in the first example, but populated in the second.
public string Location {get;set;}
}

1 Comment

Sadly, this is not true (bitter voice of experience). Doing this will result in Exception thrown: 'System.IndexOutOfRangeException' in System.Data.dll If you catch the error and continue, then it will still work, but performance becomes horrible. I have a case where it increases execution from 290 ms to 4 seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.