Today I was trying to save list of entities those were generated by DBML
This code shows how you can store your list in XML string and put it in ViewState, after that you get your list back.
Using the Code
You can use this code as you need.
Here is class
Serializator
public class Serializator
{
public static string SerializeLinqList<T>(List<T> list)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(List<T>));
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
dcs.WriteObject(writer, list);
}
return sb.ToString();
}
public static List<T> DeserializeLinqList<T>(string xml)
{
List<T> list;
DataContractSerializer dcs = new DataContractSerializer(typeof(List<T>));
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
list = dcs.ReadObject(reader) as List<T>;
}
if (list == null) list = new List<T>();
return list;
}
}
Here is how it works
public List<sp_LoadCustomDataResult> Items
{
get
{
if(ViewState["Items"] == null)
return new List<sp_LoadCustomDataResult>();
string xml = (string)ViewState["Items"];
return Serializator.DeserializeLinqList<sp_LoadCustomDataResult>(xml);
}
set
{
ViewState["Items"] = Serializator.SerializeLinqList<sp_LoadCustomDataResult>(value);
}
}
Thank you for your attention. Sorry for my poor english.