0

I want to execute a raw SQL query as shown below:

select material, method, count(*) 
from treatment 
group by material, method

and return it as a JSON object.

Because EF 6 allows the execution of raw query (i.e., Database.SQLQuery()), I use it to create a JSON Object.

My code looks like this.

// GET: Dummy
public string Dummy()
{
    string query = "select material, method, count(*) as cnt from Treatment "
                   + " group by material, method";
    var result = db.Database.SqlQuery<List<String>>(query);
    var jsonResult = JsonConvert.SerializeObject(result);

    return jsonResult;
}

However, instead of getting a JSON object with the material and methods, I get an empty json object instead.

[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]

Is it possible to return the correct JSON object without having the model for the the raw query? If so, how to update my code to return the correct result?

Thanks in advance

6
  • Are you sure your result has data in it? By the looks of it, it appears that the result was empty? I know it sounds obvious, but just step through the code and check if result contains anything? Commented Aug 23, 2018 at 10:42
  • Yes. There are several row of data in the table and the amount of empty JSON object created is the same number of rows of the result. Commented Aug 23, 2018 at 10:52
  • try chaning db.Database.SqlQuery<List<String>>(query) to db.Database.SqlQuery<List<object>>(query). Commented Aug 23, 2018 at 11:02
  • I still get the same result Commented Aug 23, 2018 at 11:06
  • 1
    List<String> only returns single list of strings while your query has 3 data sets. What do you mean of "without having the model" - are you don't want to create additional class with properties to handle query result before converting to JSON? Commented Aug 23, 2018 at 11:48

3 Answers 3

2

You can't resolve that query to a list of strings, you need to resolve it to a (list of) class(es), like this :

public static string Dummy()
{
    using (var db = new TestDbEntities())
    {
        string query = "select 'material' as material , 'method' as method, 1  as cnt  ";
        var result = db.Database.SqlQuery<MyClass>(query);
        var res = result.ToList();
        var jsonResult = JsonConvert.SerializeObject(res);
        return jsonResult;
    }
}

where your class will be something like this :

public class MyClass
{
    public string Material { get; set; }

    public string Method { get; set; }

    public int Cnt { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to capture query result sets with multiple columns using a class with matching property names (make sure they have exactly same name and proper casing) and parameterless constructor:

public string Dummy()
{
    string query = "select material as Material, method as Method, count(*) as Cnt from Treatment group by material, method";
    var result = db.Database.SqlQuery<ResultSet>(query).ToList();
    var jsonResult = JsonConvert.SerializeObject(result);
    return jsonResult;
}

public class ResultSet
{
    public string Material { get; set; }

    public string Method { get; set; }

    public int Cnt { get; set; }
}

Note that Database.SqlQuery<T> will return an IEnumerable<T> with type System.Data.Entity.Internal.InternalSqlQuery<T>, so that a List<string> is not fit in this context. If you just return single result set, Database.SqlQuery<string>(...).ToList() should be used instead of Database.SqlQuery<List<string>>.

Similar issue:

Entity framework raw SQL Query

Comments

0

Use FOR JSON AUTO:

     string query = "select material, method, count(*) as cnt from Treatment "
        + " group by material, method FOR JSON AUTO";

Comments

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.