string @" " id" ":" "1" "," " title" ":" "Messagetitle1" "" Content "": "" HelloWorld "", " " "" "" "" "" Web.abc "" }, {" id" ":" "2" "," "title" ":" "Messagetitle2" "," "Content" ": " "" "" "" URL "": "" http://web.abc/portal/hldjh/201404/t20140423_73322.htm "" } ] } }";
JSON String
/// <summary> ///DataTable conversion to list collection/// </summary> /// <typeparam name= "TResult" >type</typeparam> /// <param name= "DT" >DataTable</param> /// <returns></returns> Public StaticList<tresult> tolist<tresult> (DataTable DT)whereTResult:class,New() { //Create a list of propertiesList<propertyinfo> prlist =NewList<propertyinfo>(); //Gets the entry of the TResult type instance reflectionType T =typeof(TResult); //get all the public properties of TResult and find the same property (PropertyInfo) for the TResult property and the column name of the DataTable and add to the property listArray.foreach<propertyinfo> (T.getproperties (), p = = {if(dt. Columns.indexof (p.name)! =-1) prlist. ADD (P); }); //Create a returned collectionList<tresult> oblist =NewList<tresult>(); foreach(DataRow rowinchdt. Rows) {//Create an instance of TResultTResult OB =NewTResult (); //find the corresponding data and assign a valuePrlist. ForEach (p = = {if(Row[p.name]! = DBNull.Value) p.setvalue (ob, Row[p.name],NULL); }); //Into the returned collection. oblist. ADD (OB); } returnoblist; }datatable->list
varresult = fromAccountinchdb. Publicaccount.tolist ()whereAccount . Name.contains (keyword)SelectAccount ; Result=result. ToList (); Jobject JSON=NewJobject (NewJproperty ("accounts", NewJarray (//Account Arrayresult. Select (R=NewJobject (//Account Object NewJproperty ("ID", r.id),NewJproperty ("name", R.name),NewJproperty ("desc", R.desc)) ) ) ) );Linq JSON