Nhibernate返回带有列表的Json

时间:2019-02-01 12:40:11

标签: c# json nhibernate

我的C#代码有问题。我不能产生我JSON很好。

我正在使用NHIBERNATE连接并从SQL Server中选择数据。我想生成一个带有列表对象的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;

班级:

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>

,并更改班级。但是我遇到了错误

  

列表不包含LoginId的定义

在新班级:

public List<PartnerUsersss> PartnerUsers { get; set; }

1 个答案:

答案 0 :(得分:0)

您需要从查询中删除联接并按如下所示修改查询,

template<typename T>
void f(T&& n)
{
    ++n; // ok to modify a const object, why?
}

template<typename T>
void g()
{
    int n{};
    f<const T&>(n);
}

int main()
{
    g<int&>();
}

您需要将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