1 public string GetAllCity(int provinceid) 2 { 3 DAL.ProCityTown procitytown = new ProCityTown(); 4 IList<City> list = procitytown.GetAllCity(provinceid); 5 string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式 6 return strjson; 7 }
1 public IList<City> GetAllCity(int provinceid) 2 { 3 IList<City> list = null; 4 using (ISession session = this.factoy.OpenSession()) 5 { 6 ITransaction tran = session.BeginTransaction(); 7 list = session.QueryOver<City>() 8 .Where(c => c.Province.ProvinceID == provinceid) 9 .List(); 10 11 12 13 tran.Commit(); 14 session.Close(); 15 16 return list; 17 18 } 19 }
DAL:
public IList<Province> GetAllProvince()
{
IList<Province> list = null;
using (ISession session = this.factoy.OpenSession())
{
ITransaction tran = session.BeginTransaction();
list = session.QueryOver<Province>()
.List();
tran.Commit();
session.Close();
return list;
}
}
[Test]
public IList<City> GetAllCity(int provinceid)
{
IList<City> list = null;
using (ISession session = this.factoy.OpenSession())
{
ITransaction tran = session.BeginTransaction();
list = session.QueryOver<City>()
.Inner.JoinQueryOver<Province>(c => c.Province)
.Where(p => p.ProvinceID == provinceid)
.List();
tran.Commit();
session.Close();
}
return list;
}
public IList<DistrictDTO> GetAllTown(int cityid)
{
DistrictDTO dto = null;
IList<DistrictDTO> retList = null;
using (ISession session = this.factoy.OpenSession())
{
ITransaction tran = session.BeginTransaction();
// list = session.QueryOver<District>()
// .Inner.JoinQueryOver<City>(d => d.City)
//.Where(c => c.CityID == cityid)
//.List();
// tran.Commit();
// session.Close();
retList = session.QueryOver<District>()
.Inner.JoinQueryOver<City>(d => d.City)
.Where(c => c.CityID == cityid)
.SelectList(list => list
.Select(d => d.DistrictID).WithAlias(() => dto.DistrictID)
.Select(d => d.DistrictName).WithAlias(() => dto.DistrictName)
.Select(d => d.City).WithAlias(() => dto.City)
)
.TransformUsing(Transformers.AliasToBean<DistrictDTO>())
.List<DistrictDTO>();
tran.Commit();
session.Close();
}
return retList;
}
BLL:
public string GetAllProvince()
{
DAL.ProCityTown procitytown = new ProCityTown();
IList<Province> list = procitytown.GetAllProvince();
string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式
return strjson;
}
public string GetAllCity(int provinceid)
{
DAL.ProCityTown procitytown = new ProCityTown();
IList<City> list = procitytown.GetAllCity(provinceid);
string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式
return strjson;
}
public string GetAllTown(int cityid)
{
DAL.ProCityTown procitytown = new ProCityTown();
IList<DistrictDTO> list = procitytown.GetAllTown(cityid);
string strjson = JsonConvert.SerializeObject(list, Formatting.Indented);//将对象u转化为json格式
return strjson;
}
用NHibernate实现的省市县三级联动,在bll的getalltown时会报错得到的list是readonly
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = oSerializer.Serialize(oList);
http://www.baidu.com/s?wd=how+to+list+type+to+json+in+C%23&rsv_bp=0&rsv_spt=3&rsv_n=2&rsv_sug3=1&rsv_sug1=1&rsv_sug4=42&inputT=609
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e1) { List<Employee> eList = new List<Employee>(); Employee e = new Employee(); e.Name = "Minal"; e.Age = 24; eList.Add(e); e = new Employee(); e.Name = "Santosh"; e.Age = 24; eList.Add(e); string ans = JsonConvert.SerializeObject(eList, Formatting.Indented); string script = "var employeeList = {\"Employee\": " + ans+"};"; script += "for(i = 0;i<employeeList.Employee.length;i++)"; script += "{"; script += "alert ('Name : ='+employeeList.Employee[i].Name+' Age : = '+employeeList.Employee[i].Age);"; script += "}"; ClientScriptManager cs = Page.ClientScript; cs.RegisterStartupScript(Page.GetType(), "JSON", script, true); } } public class Employee { public string Name; public int Age; }
http://www.codeproject.com/Articles/78928/Create-JSON-from-C-using-JSON-Library
没用的,因为list是readonly只能读,不能改
检查下对象是否可以被序列化
上面的省province和市city的list都可以序列化,但是就是区district不能,困惑了我好几天了,就是报所得到的distric的list是readonly,在NHibernate3.3的api有一章讲的是readonly Entity,里面好像是单一或者单向关联查询后得到的数据自动为readonly,我的表关联是这样的,city many-to-one province,District many-to-one city。求大神解决,我可以发源码。