-1

this code:

using (DataClassesDataContext db = new DataClassesDataContext())
{
    IEnumerable<view_subject> results = db.ExecuteQuery<view_subject>(query);

    if (dataBind)
    {
        GridViewSearchResults.DataSource = results;
        GridViewSearchResults.DataBind();
    }

    return results.Count();
}

Throws me an exception: The query results cannot be enumerated more than once. And I dont understand why? Can somebody please help me?

DefinitionHigh
  • 164
  • 1
  • 3
  • 14
  • 1
    Did you look for duplicate questions before asking? Look down the "related" questions at the side of this... – Jon Skeet Jul 29 '14 at 18:05
  • there is similar question on SO http://stackoverflow.com/questions/5723555/the-result-of-a-query-cannot-be-enumerated-more-than-once – Vlad Bezden Jul 29 '14 at 18:17

1 Answers1

1

Using ToList() will retrieve all records from DB, and now it works on data instead of database query.

using (DataClassesDataContext db = new DataClassesDataContext())
{
    List<view_subject> results = db.ExecuteQuery<view_subject>(query).ToList();

    if (dataBind)
    {
        GridViewSearchResults.DataSource = results;
        GridViewSearchResults.DataBind();
    }

    return results.Count();
}
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179