0

I have one query for create chart in Controller;

select top 10 ParcaMakineAdi,Count(ParcaMakineAdi) from MakinelerVeParcalar group by ParcaMakineAdi

List returns 10 rows, each row contains String,Int

I want convert this query to a list and send it to View.

But I can't get list. For example (I want) ;

string query = "select top 10 ParcaMakineAdi as 'Name',Count(ParcaMakineAdi) as 'Value' from MakinelerVeParcalar group by ParcaMakineAdi";

var datas = query.toList();

string name = datas[0].Name;
int value = datas[0].Value;

is this scenario possible?

0

1 Answer 1

2

If you're using EF you can do it like this:

var items = context.MakinelerVeParcalar.GroupBy(g => g.ParcaMakineAdi).Select(s => new { name = s.ParcaMakineAdi, count = s.Count() }).Take(10);

var datas = query.toList();

If you're not using any ORM you can do something like this:

List<String> datas = new List<String>();

using(SqlConnection connection = new SqlConnection("conn_string"))
{
    connection.Open();
    string query = "select top 10 ParcaMakineAdi as 'Name',Count(ParcaMakineAdi) as 'Value' from MakinelerVeParcalar group by ParcaMakineAdi";
    using(SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                datas.Add(reader.GetString(0));
            }         
        }
    }
}

EDIT: Actually I miss typed that example which would just return the name but I think you can easily adjust to your needs.

Sign up to request clarification or add additional context in comments.

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.