1

I have a simple gridview which I need to bind with a list of users.

In EF.5.0 I could simple write

context.Users.Select(emp => new { Name = emp.FirstName, EmailId = emp.EmailId, UserId = emp.UserId }).ToList();

However, I don't see the .ToList() method anymore in EF6.0

So, I have to write an async query using ToAsyncList(). However, not sure why the below code does not work and system goes in endless execution.

protected void Page_Load(object sender, EventArgs e)
{
    var task = LoadData();
    task.Wait();
    GridView1.DataSource = task.Result;
    GridView1.DataBind();
}

private async Task<List<User>> LoadData()
{
    List<User> users = null;

    using (var context = new BlogEntities())
    {
        users = await context.Database.SqlQuery<User>("Select * from User", new object[] { }).ToListAsync();
    }

    return users;
}

Can anyone please let me know, what I am doing wrong here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Srikant Sahu
  • 839
  • 1
  • 6
  • 16

1 Answers1

2

Use this code:

var users = context.Users.SqlQuery("SELECT * FROM dbo.User").ToList(); 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RezaDefaei
  • 76
  • 6
  • Thanks. This one works. but if I use context.Database.SqlQuery(User.GetType(), "Select * from [User]", new object[] {}).ToListAsync(), the async method does not work . I am wondering why ToList() method is not available here and it forces the users to use async. – Srikant Sahu Nov 27 '16 at 19:17
  • `ToListAsync()` does not work or it takes long time to execute? There is a [topic](http://stackoverflow.com/questions/28543293/entity-framework-async-operation-takes-ten-times-as-long-to-complete) regarding this method – Jakub Jankowski Nov 27 '16 at 19:50