1

我有这样的课程:

public class BigClass
{
    public int Id {get; set;}
    public List<First> Firsts {get; set;}
}

public class First
{
    public int Id {get ;set;}
    public int BigClassId {get; set;}
    public int Name {get; set;}
    public List<Second> Seconds {get; set;} 
    public List<Third> Thirds {get; set;}
    public List<Fourth> Fourths {get; set;}
}

public class Second
{
    public int Id {get; set;}
    public string Name {get; set;}
    public string Code {get; set;}
}

public class Third
{
    public int Id {get; set;}
    public string Code {get; set;}
}

public class Fourth
{
    public int Id {get; set;}
    public string Code {get; set;}
}

Database - BigClass

id_bigclass      
-----------

    1 

Database - First

id_first     id_bigclass          name_first
--------     -----------          ----------
 
    1            1                   hero


Database - Second

id_second      id_code       first1_name
--------        -------       ----------
   1              3333           hero
   2              4444           hero
   3              8888           villian
Database - Third

id_third       id_code
--------        -------
   1              3333
   2              4444
   3              6666


Database - Fourth
id_fourth       Code
--------        -------
    1            3333
    2            4444
    3            5555

所以一个大类有几个第一类实例。第一类有几个第二类、第三类、第四类实例。但是 second.name = first.name; 第三个代码=第二个代码;第四个代码=第二个代码;

我写了 dapper multimapping 但它返回的内容取决于 first3 列表计数未插入到头等舱的列表中。

var sql = "select * from bigclass as bc " +
                "LEFT JOIN first AS fr on fr.id_bigclass = bc.id " +
                "LEFT JOIN second AS sc on sc.Name = fr.Name " +
                "LEFT JOIN third AS th on th.Code = sc.Code " +
                "LEFT JOIN fourth AS fo on fo.Code = sc.HandCode ";


var bigClass = conn.Query<BigClass>(sql, new[] { typeof(BigClass), typeof(First), typeof(Second), typeof(Third), typeof(Fourth) }, obj =>
                {
                    BigClass bigClass= obj[0] as BigClass;
                    First first = obj[1] as First;
                    Second second = obj[2] as Second;
                    Third third = obj[3] as Third;
                    Fourth fourth = obj[4] as Fourth;
                    bigClass.Firsts = bigClass.Firsts ?? new List<First>();
                    First.Seconds.Add(second); First.Third.Add(third); First.Fourth.Add(fourth);
                    return bigClass;}
                 , splitOn: "id").FirstOrDefault();

我的预期结果是 BigClass Firsts 列表计数为 1,First classes Seconds 列表计数为 2,Third 计数为 2,Fourth 计数为 2。怎么办?

4

0 回答 0