LINQ Entity to store in ViewState or Serialize
2009-08-17 17:34
295 查看
Recently, I was trying to store some temp data in ViewState, and I was using LINQ's Entity, and I found it errored by couldn't serialized the LINQ's entity. After some searching, I found it needed to set a property of DataContext file calling 'Serialization Mode' to value 'Unidirectional', this seems it was specified for WCF, for the setting will add [DataContact],[DataMember] attributes for class or class's properties.
After that, You'll using class 'DataContractSerializer' to do serialization.
DataContractSerializer dcs = new DataContractSerializer(typeof(List<user>));
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
dcs.WriteObject(xw, comments);
xw.Close();
ViewState["ds"] = sb.ToString();
when you want to deserialize from ViewState, using
string content = ViewState["ds"] as string;
DataContractSerializer dsc = new DataContractSerializer(typeof(List<user>));
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(content));
object obj = dsc.ReadObject(ms);
the above codes, seem not so good, so, any comment is welcome for better solution.
After that, You'll using class 'DataContractSerializer' to do serialization.
DataContractSerializer dcs = new DataContractSerializer(typeof(List<user>));
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
dcs.WriteObject(xw, comments);
xw.Close();
ViewState["ds"] = sb.ToString();
when you want to deserialize from ViewState, using
string content = ViewState["ds"] as string;
DataContractSerializer dsc = new DataContractSerializer(typeof(List<user>));
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(content));
object obj = dsc.ReadObject(ms);
the above codes, seem not so good, so, any comment is welcome for better solution.
相关文章推荐
- Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive
- Cannot serialize Object to ViewState only Session
- How to add or list certificates from keystore or trustStore in Java - Keytool Example Read more: ht
- How to repair redis cluster inconsistent or unstable state ?
- How to modify non-customizable entity in MS Dynamics CRM 4.0 (Add Product price column to Price List Item view)
- The specified type member 'IsLock' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
- 学习并使用了两种linq to entity 的实现sql关键字in的查询方法
- How To Store State in Active Server Pages Applications
- 学习并使用了两种linq to entity 的实现sql关键字in的查询方法
- 将viewstate存储在数据库中并压缩 附代码(store viewstate in database and compress)
- How to store CGPoint or CGRect in NSArray?
- How to view the schema class or attribute definition you first have to install the Active Directory Schema snap-in
- LinQ To ViewState
- Linq to Entity 动态拼接查询条件(重点是OR)
- The entity cannot be constructed in a LINQ to Entit
- [Angular] How to get Store state in ngrx Effect
- The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
- 【整理】Linq to Entity 动态拼接查询条件(重点是OR)
- How to cal Session_End() method when you store the Session state not in Inprc but StageServer, etc.
- using sql server or sql azure for session state store in asp.net