rlcrews
rlcrews

Reputation: 3562

How to query data within a session variable using linq

within an asp.net webform project I have a session variable that I am populating with data from my business object. This object contains Icollections which in turn are bound to gridviews.

   myBase.GetAssocMInvolvedPeople(); //call to Business object
   if (myBase.AssocMInvolvedPeople != null)
    {
     Session["sessBase"] = myBase; //sync session with object results 
     gv_Names.DataSource = myBase.AssocMInvolvedPeople; //bind Icollection to grid
     gv_Names.DataBind();
     gv_Names.Visible = true;
    }

Then within my row selection on the grid I retrieve the object id of the selected row

GridDataItem selectedItem = (GridDataItem)gv_Names.SelectedItems[0]; 
SecondaryID = selectedItem["ObjectId"].Text;

The goal is to take the object Id and query the session variable (sessBase) to retrieve the remaining values (that were not visible in the gird) of the collection and display them in the UI in a form/textfield format. I would think the best approach for this would be to use Linq to query the session variable but I am stuck here on how to go about this or accessing the nested collection (AssocMInvolvedPeople) .

I would assume that to set this up in the method I would reset my object back to the session variable as:

protected void GetAdditionalData()
{
 myBase = (BusinesObjectName)Session["sessBase"]; 

//here is where I am stuck how to query this object and select the records based upon the selected ID 
    ...
    }

I would appreciate any tips or suggestions on the best approach to retrieving this data

Thank you,

Upvotes: 0

Views: 2521

Answers (2)

Jason Meckley
Jason Meckley

Reputation: 7591

remove session altogether and query the database directly. Session only complicates the workflow and increases memory usage.

The data item is also stored in viewstate when bound to the grid, so you could pull the underlying data structure from the bound row.

in either case, don't use session for this scenario.

Upvotes: 1

lsuarez
lsuarez

Reputation: 4992

If you type-cast the Session object and put it in a closure, you should be able to use it or any collection property in the object in a standard LINQ query, i.e.:

var results =
    from MyClass i in (Session["sessBase"] as MyBusinessObject).MyCollection
    where secondaryID = i.ID
    select i

I realize I've made some arbitrary determinations about how you've structured the business object, but without knowing more, I think this should sufficiently demonstrate the concept.

Upvotes: 0

Related Questions