1

Following is the code

SPList splist = web.Lists["MyList"];

var query = from SPListItem oItem in splist.Items.OfType<SPListItem>()
                            where !Convert.ToString(oItem["Status"]).Equals("Completed")
                            select oItem;

List<SPListItem> value = query.ToList<SPListItem>();

upto here working perfectly..

Now store value into viewstate getting following exception

ViewState.Add("Key", value);

Exception

System.Runtime.Serialization.SerializationException: Type 'Microsoft.SharePoint.SPListItem' in Assembly 'Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable.

Thanks

Arsalan Adam Khatri
  • 14,531
  • 3
  • 36
  • 59
Hitesh Chandegara
  • 2,638
  • 6
  • 29
  • 49
  • The result of this query is SPListItems which are not serializable and cannot be added to the ViewState. This query may return a large amount of items and it is not recommended to save them in ViewState. What are you trying to achieve? Maybe there is a better way. –  Dec 05 '12 at 05:55
  • i want get same linq query result during button click event bcz i don't trip to database(SPList) again. – Hitesh Chandegara Dec 05 '12 at 13:53
  • @hitz Do the query again on button click instead of saving in ViewState! – Arsalan Adam Khatri Dec 05 '12 at 14:11
  • You really should be running a CAML query. Using linq fetches all the items in the list before filtering them. If you have thousands of items, this will kill the server – Ralph W Dec 05 '12 at 16:40

0 Answers0