Passing Linq query results to a different function

Hi,

I have the following linq code within my app

var results = (from t in query
where t.ProjectID == new Guid(this.Session[“ProjectID”].ToString())
orderby t.Number ascending
select new
{
ID = t.ID,
Tag = t.Number,
Description = t.Description,
SubSystems = GeneralHelper.TagSubSystems(t),
Module = t.Area.Module.Name,
Discipline = t.Discipline.Name,
ITRs = GeneralHelper.TagITRsHTML(t)
});

I want to pass the query result to another function to handle my paging. I currently have another function defined like this

public void BindGridData(IQueryable result)
{
gridView.DataSource = result;
gridView.DataBind();
}

It works like this, but the problem is I cannot do “take” or “skip” on “result” because it’s declared as IQueryable. Is there a work around to handle this?

Thanks

there was not TObject for cast options, I ended up going this route

public void BindGridData<T>(IEnumerable<T> result)
{
gridView.DataSource = result;
gridView.DataBind();
}

which worked.

Thanks for the help.

Couldn’t you just call AsEnumerable on the query before you pass it and have the method accept an IEnumerable?

Alternatively you should be able cast the elements of the IQueryable result to whatever object ‘t’ is (on the IQueryable inside BindGridData) and then call AsEnumerable.

{
    var results = (query.Where(t => t.ProjectID == new Guid(this.Session["ProjectID"].ToString()))
                        .OrderBy(t => t.Number)
                        .Select(t => new {   
                                ID = t.ID,
                                Tag = t.Number,
                                Description = t.Description,
                                SubSystems = GeneralHelper.TagSubSystems(t),
                                Module = t.Area.Module.Name,
                                Discipline = t.Discipline.Name,
                                ITRs = GeneralHelper.TagITRsHTML(t)
                                })
                          );

    BindGridData(results);
}

public void BindGridData(IQueryable result)
{
    var enumerableResult = result.Cast<TObject>.AsEnumerable();
}

TObject being whatever t in your query is.