C# 简洁映射的多重关系

C# 简洁映射的多重关系,c#,dapper,C#,Dapper,这是我的模型: public class Word { public string Word1 { get; set; } public string SpecialCases { get; set; } public virtual ICollection<Defination> Definations { get; set; } } public class Defination { public long Wor

这是我的模型:

public class Word
{
    public string Word1 { get; set; }               
    public string SpecialCases { get; set; }

    public virtual ICollection<Defination> Definations { get; set; }
}

public class Defination
{
    public long WordId { get; set; }        
    public string Subjects { get; set; }    
    public virtual Word Word { get; set; }        

    public virtual ICollection<Example> Examples { get; set; }
}

public class Example
{
    public long DefinationId { get; set; }            
    public string English { get; set; }        

    public virtual Defination Defination { get; set; }
}
公共类单词
{
公共字符串Word1{get;set;}
公共字符串专用代码{get;set;}
公共虚拟ICollection定义{get;set;}
}
公共类定义
{
公共长字ID{get;set;}
公共字符串主题{get;set;}
公共虚拟字{get;set;}
公共虚拟ICollection示例{get;set;}
}
公开课范例
{
公共长定义ID{get;set;}
公共字符串英语{get;set;}
公共虚拟定义{get;set;}
}
我想使用dapper将结果映射到模型。这是我使用它的代码

string query = 
    $"SELECT Word.*, Defination.* " + 
    $"FROM Word Left Join Defination On Word.id = Defination.Id " +                    
    $"WHERE Word.id = {id} ";                   
    var item2 = cn.Query<Word, Defination, Word>(query,
        (Word, Defination) =>
            {                            
                Word.Definations.Add(Defination);
                Defination.Word = Word;                            
                return Word;
            }
        );
    return item2.FirstOrDefault();
字符串查询=
$“选择单词。*,定义。*”+
$“从Word左连接Word.id上的定义=Defination.id”+
$“其中Word.id={id}”;
var item2=cn.Query(查询,
(单词,定义)=>
{                            
单词。定义。添加(定义);
定义:单词=单词;
返回词;
}
);
返回item2.FirstOrDefault();

我的问题是如何向每个定义模型添加示例数据。

我通过以下代码解决了我的问题:

public Word GetByID(long id)
        {
            using (IDbConnection cn = Connection)
            {
                cn.Open();
                const string wordQuery = "SELECT * FROM Word WHERE Id = @Id AND Word.SoftDelete = 0 ";
                Word _word = cn.Query<Word>(wordQuery, new { Id = id }).FirstOrDefault();
                if(_word != null && _word.Id != 0)
                {
                    const string definationQuery = "SELECT * FROM Defination where WordId = @WordId AND SoftDelete = 0";
                    List<Defination> _defination = cn.Query<Defination>(definationQuery, new { WordId = _word.Id }).ToList();
                    _word.Definations = _defination;
                    if(_defination != null && _defination.Any())
                    {
                        const string exampleQuery = "SELECT * FROM Example where DefinationId = @DefinationId AND SoftDelete = 0";
                        for (int i=0; i<_defination.Count(); i++ )
                        {
                            _defination[i].Examples = cn.Query<Example>(exampleQuery, new { DefinationId = _defination[i].Id }).ToList();                            
                        }                        
                    }
                }

                return _word;
            }
        }
公共字GetByID(长id)
{
使用(IDbConnection cn=Connection)
{
cn.Open();
const string wordQuery=“从Word中选择*,其中Id=@Id和Word.SoftDelete=0”;
Word _Word=cn.Query(wordQuery,new{Id=Id}).FirstOrDefault();
if(_word!=null&&u word.Id!=0)
{
const string definationQuery=“从定义中选择*,其中WordId=@WordId和SoftDelete=0”;
List _defination=cn.Query(definationQuery,new{WordId=_word.Id}).ToList();
_定义=_定义;
if(_defination!=null&&u defination.Any())
{
const string exampleQuery=“从示例中选择*,其中DefinationId=@DefinationId和SoftDelete=0”;
对于(int i=0;i