休眠到 return Json with list
Nhibernate to return Json with list
我的 C# 代码有问题。我的 JSON 做不好。
我正在使用 NHIBERNATE 从我的 SQL 服务器连接和 select 数据。我想生成一个 JSON 里面有一个列表对象。需要你们的帮助。
NHibernate 代码:
var query = (from partners in session.Query<Partners>()
join partnerUsers in session.Query<PartnerUsers>() on partners.PartnerId equals partnerUsers.PartnerId
where partners.PartnerId == partnerId
select new Partnersss
{
PartnerId = partners.PartnerId,
PartnerName = partners.Name,
PartnerUsers = new PartnerUsersss()
{
LoginId = partnerUsers.LoginId
}
}).ToList<object>();
return query;
Class:
public class PartnerUsersss
{
public int PartnerUserId { get; set; }
public string LoginId { get; set; }
}
public class Partnersss
{
public int PartnerId { get; set; }
public string PartnerName { get; set; }
public PartnerUsersss PartnerUsers { get; set; }
}
JSON 结果:
{
"Data": "",
"data": [{
"PartnerId": 1,
"PartnerName": "ExpressPay",
"PartnerUsers": {
"PartnerUserId": "0",
"LoginId": "a@a.com"
}
}, {
"PartnerId": 1,
"PartnerName": "ExpressPay",
"PartnerUsers": {
"PartnerUserId": "0",
"LoginId": "b@b.com"
}
}],
"ResponseCode": "0",
"ResponseMessage": "Successful"
}
但我希望能够生成此 JSON:
{
"Data": "",
"data": [{
"PartnerId ": 1,
"PartnerName": "ExpressPay ",
"PartnerUsers": [{
"partnerUserId": "0",
"loginId": "a@a.com"
}, {
"partnerUserId": "0",
"loginId": "b@b.com"
}]
}],
"ResponseCode": "0",
"ResponseMessage": "Successful"
}
我尝试将 List 与这种代码一起使用
PartnerUsers = new List<PartnerUsersss>
并更改 class。但是我得到一个错误
List does not contain a definition for LoginId
新 class:
public List<PartnerUsersss> PartnerUsers { get; set; }
您需要从查询中删除连接并修改您的查询,
var query = (from partners in session.Query<Partners>()
where partners.PartnerId == partnerId
let partnerUsers = (from pu in session.Query<PartnerUsers>()
where pu.PartnerId == partners.PartnerId
select pu).ToList()
select new Partnersss
{
PartnerId = partners.PartnerId,
PartnerName = partners.Name,
PartnerUsers = partnerUsers
}).ToList();
您需要将 PartnerUsers
属性 的类型更改为列表,
public List<PartnerUsersss> PartnerUsers { get; set; }
我的 C# 代码有问题。我的 JSON 做不好。
我正在使用 NHIBERNATE 从我的 SQL 服务器连接和 select 数据。我想生成一个 JSON 里面有一个列表对象。需要你们的帮助。
NHibernate 代码:
var query = (from partners in session.Query<Partners>()
join partnerUsers in session.Query<PartnerUsers>() on partners.PartnerId equals partnerUsers.PartnerId
where partners.PartnerId == partnerId
select new Partnersss
{
PartnerId = partners.PartnerId,
PartnerName = partners.Name,
PartnerUsers = new PartnerUsersss()
{
LoginId = partnerUsers.LoginId
}
}).ToList<object>();
return query;
Class:
public class PartnerUsersss
{
public int PartnerUserId { get; set; }
public string LoginId { get; set; }
}
public class Partnersss
{
public int PartnerId { get; set; }
public string PartnerName { get; set; }
public PartnerUsersss PartnerUsers { get; set; }
}
JSON 结果:
{
"Data": "",
"data": [{
"PartnerId": 1,
"PartnerName": "ExpressPay",
"PartnerUsers": {
"PartnerUserId": "0",
"LoginId": "a@a.com"
}
}, {
"PartnerId": 1,
"PartnerName": "ExpressPay",
"PartnerUsers": {
"PartnerUserId": "0",
"LoginId": "b@b.com"
}
}],
"ResponseCode": "0",
"ResponseMessage": "Successful"
}
但我希望能够生成此 JSON:
{
"Data": "",
"data": [{
"PartnerId ": 1,
"PartnerName": "ExpressPay ",
"PartnerUsers": [{
"partnerUserId": "0",
"loginId": "a@a.com"
}, {
"partnerUserId": "0",
"loginId": "b@b.com"
}]
}],
"ResponseCode": "0",
"ResponseMessage": "Successful"
}
我尝试将 List 与这种代码一起使用
PartnerUsers = new List<PartnerUsersss>
并更改 class。但是我得到一个错误
List does not contain a definition for LoginId
新 class:
public List<PartnerUsersss> PartnerUsers { get; set; }
您需要从查询中删除连接并修改您的查询,
var query = (from partners in session.Query<Partners>()
where partners.PartnerId == partnerId
let partnerUsers = (from pu in session.Query<PartnerUsers>()
where pu.PartnerId == partners.PartnerId
select pu).ToList()
select new Partnersss
{
PartnerId = partners.PartnerId,
PartnerName = partners.Name,
PartnerUsers = partnerUsers
}).ToList();
您需要将 PartnerUsers
属性 的类型更改为列表,
public List<PartnerUsersss> PartnerUsers { get; set; }