SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot chieftain's Avatar
    Join Date
    Feb 2005
    Location
    Michigan
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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

  2. #2
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Jersey
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    {
        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.
    Matt Daly

  3. #3
    SitePoint Zealot chieftain's Avatar
    Join Date
    Feb 2005
    Location
    Michigan
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •