ashkufaraz
ashkufaraz

Reputation: 5307

get value Session

DataClassesDataContext dc = new DataClassesDataContext();
var summaryFieldDB = from b in dc.PropertyCompanies where (b.SummaryField.Contains(txtSearch)) select b;

Session["summaryField"] = summaryFieldDB;

now how can access to field summaryFiledDB???

if use this

  System.Data.Linq.Table<PropertyCompany> result = (System.Data.Linq.Table<PropertyCompany>)Session["summaryField"];

This line when run program say error:Unable to cast object of type 'System.Data.Linq.DataQuery1[PropertyCompany]' to type 'System.Data.Linq.Table1[PropertyCompany]

if i use Store Procedure Like FullSearch

Session["search"]   = dc.FullSearch("anv", true, true, true, true, true, true, true, true, true, true, true);
System.Data.Linq.ISingleResult<FullSearchResult> b = (System.Data.Linq.ISingleResult<FullSearchResult>)Session["search"];
  foreach(var item in b)
  {
   //work with fields
  }

this work fine!!!

But I do not want to work with the Stored Procedure

Upvotes: 1

Views: 282

Answers (3)

saj
saj

Reputation: 4816

Linq to Sql uses deferred Loading (lazy loading) meaning no data is executed until you finish off your expression with .ToList() etc, i.e. specify how you want your data

Change this to (Added ToList)

var summaryFieldDB = (from b in dc.PropertyCompanies where (b.SummaryField.Contains(txtSearch)) select b).ToList()

And (cast with List)

  List<PropertyCompany> result = (List<PropertyCompany>)Session["summaryField"];

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064144

I query isn't a table. IMO, you should settle on something like a List<T> here:

Session["summaryField"] = summaryFieldDB.ToList();

and

var result = (List<PropertyCompany>)Session["summaryField"];

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You have to do like...

List<PropertyCompany> result = (List<PropertyCompany>)Session["summaryField"];

Since you are store the collection of type PropertyCompany

Upvotes: 1

Related Questions