Dapper ORM 多层嵌套对象

Dapper ORM multiple level nested object

我正在尝试 return SQL 使用具有嵌套对象的 Dapper ORM 服务器数据,到目前为止,这就是我正在做的事情

型号class

public class CustomerAll
{
    public string KODELANG { get; set; }
    public string GRUPLANG { get; set; }
    public string NAMALANG { get; set; }
    public string ALAMAT { get; set; }
    public string STATUS { get; set; }
    public List<NoDiscount> DISC_ACTIVE  { get; set; }
}

public class NoDiscount
{
    public string NO_DISC { get; set; }
    public List<ProductDiscount> LIST_PRODUCT{ get; set; }
}

public class ProductDiscount
{
    public string KODEPROD{ get; set; }
}

原始查询

SELECT CUSTOMER.KODELANG
      ,CUSTOMER.GRUPLANG
      ,CUSTOMER.NAMALANG
      ,CUSTOMER.ALAMAT
      ,CUSTOMER.STATUS
      ,DISCOUNT.NO_DISC
      ,PRODUCT.KODEPROD
  FROM CUST_TABLE CUSTOMER, DCNT_TABLE DISCOUNT, PRODUCT_TABLE PRODUCT
 WHERE     CUSTOMER.KODELANG = DISCOUNT.KODELANG
       AND DISCOUNT.NO_DISC = PRODUCT.NO_DISC
       AND CUSTOMER.KODELANG = @CustNum

控制器class

var parameters = new { CustNum = customerNumber };
var lookup = new Dictionary<string, CustomerAll>();

con.Query<CustomerAll, NoDiscount, ProductDiscount, CustomerAll>(
    querySql,
    (c, n, p) =>
    {
        CustomerAll customer;

        if (!lookup.TryGetValue(c.KODELANG, out customer))
        {
            lookup.Add(c.KODELANG, customer = c);
        }
        if (customer.DISC_ACTIVE == null)
        {
            customer.DISC_ACTIVE = new List<NoDiscount>();
        }
        if (p != null)
        {
            if (n.LIST_PRODUCT == null)
            {
                n.LIST_PRODUCT = new List<ProductDiscount>();
            }
            n.LIST_PRODUCT.Add(p);
        }
        customer.DISC_ACTIVE.Add(n);
        return customer;
    }
    ,parameters
    ,splitOn: "KODELANG, NO_DISC, KODEPROD");

return Ok(lookup.Values);

下面是回复 returned

[
    {
        "KODELANG": "101308",
        "GRUPLANG": "22",
        "NAMALANG": "Name Example",
        "ALAMAT": "Street number 4",
        "STATUS": "A",
        "DISC_ACTIVE": [
            {
                "NO_DISC": "DISC/1021/0001",
                "LIST_PRODUCT": [
                    {
                        "KODEPROD": "TLBCA"
                    }
                ]
            },
            {
                "NO_DISC": "DISC/1021/0001",
                "LIST_PRODUCT": [
                    {
                        "KODEPROD": "TLBCB"
                    }
                ]
            },
            {
                "NO_DISC": "DISC/3304/0009",
                "LIST_PRODUCT": [
                    {
                        "KODEPROD": "ZVKAA"
                    }
                ]
            }
        ]
    }
]

我想要的是像这样return

[
    {
        "KODELANG": "101308",
        "GRUPLANG": "22",
        "NAMALANG": "Name Example",
        "ALAMAT": "Street number 4",
        "STATUS": "A",
        "DISC_ACTIVE": [
            {
                "NO_DISC": "DISC/1021/0001",
                "LIST_PRODUCT": [
                    {
                        "KODEPROD": "TLBCA"
                    },
                    {
                        "KODEPROD": "TLBCB"
                    },
                    {
                        "KODEPROD": "TLBCC"
                    }
                ]
            },
            {
                "NO_DISC": "DISC/3304/0001",
                "LIST_PRODUCT": [
                    {
                        "KODEPROD": "ZVKAA"
                    }
                ]
            }
        ]
    }
]

如果 NO_DISC 相同,我不想再次添加 LIST_PRODUCT

试试这个,注意同一个 NoDiscount 对象可能在不同的客户中有重复的副​​本

        var parameters = new { CustNum = customerNumber };
        var customDict = new Dictionary<string, CustomerAll>();

        con.Query<CustomerAll, NoDiscount, ProductDiscount, CustomerAll>(
            querySql,
            (c, n, p) =>
            {
                CustomerAll customer = null;

                if (customDict.ContainsKey(c.KODELANG))
                {
                    customer = customDict[c.KODELANG];
                }
                else
                {
                    var customerObj = new CustomerAll()
                    {
                        KODELANG = c.KODELAN,
                        DISC_ACTIVE = new List<NoDiscount>()
                    };

                    customDict.Add(c.KODELANG, customerObj);
                };

                NoDiscount noDiscount = customer.DISC_ACTIVE.FirstOrDefault(x => x.NO_DISC == n.NO_DISC);
                if (noDiscount ==null)
                {
                    var noDiscountObj = new NoDiscount()
                    {
                        NO_DISC = n.NO_DISC,
                        LIST_PRODUCT = new List<ProductDiscount>()
                    };
                    noDiscount = noDiscountObj;
                    customer.DISC_ACTIVE.Add(noDiscountObj);
                }

                if (p != null && !noDiscount.LIST_PRODUCT.Contains(x=>x.KODEPROD==p.KODEPROD))
                {
                    noDiscount.LIST_PRODUCT.Add(p);
                }
                return customer;
            }
            , parameters
            , splitOn: "KODELANG, NO_DISC, KODEPROD");

        return Ok(customDict.Values);