C# 简洁的c型内连接
嗨,我试着用dapper做这个查询,但是列表contatos返回的值为空,我的类客户端,我不知道我做错了什么。我的类Clt_Cadclient是一个客户有几个联系人Clt_cadContatos。请帮忙C# 简洁的c型内连接,c#,dapper,C#,Dapper,嗨,我试着用dapper做这个查询,但是列表contatos返回的值为空,我的类客户端,我不知道我做错了什么。我的类Clt_Cadclient是一个客户有几个联系人Clt_cadContatos。请帮忙 var lookup = new Dictionary<int, Clt_cadCliente>(); cn.Query<Clt_cadCliente, Clt_cadContatos, Clt_cadCliente>(@"
var lookup = new Dictionary<int, Clt_cadCliente>();
cn.Query<Clt_cadCliente, Clt_cadContatos, Clt_cadCliente>(@"
SELECT s.*, a.*
FROM Clt_cadCliente s
INNER JOIN Clt_cadContatos a ON s.IdCLiente = a.IdCliente ", (s, a) =>
{
Clt_cadCliente shop;
if (!lookup.TryGetValue(s.IdCliente, out shop))
{
lookup.Add(s.IdCliente, shop = s);
}
shop.Clt_cadContatos.Add(a);
return shop;
}, splitOn: "IdCliente").AsQueryable();
var resultList = lookup.Values;
类别:
public partial class Clt_cadCliente
{
public int IdCliente { get; set; }
public Nullable<int> IdClientePai { get; set; }
public string Codigo { get; set; }
public string Nome { get; set; }
public Nullable<System.DateTime> DataNasc { get; set; }
public string Sexo { get; set; }
public Nullable<int> IdEstCivil { get; set; }
public string CPF { get; set; }
public string RG { get; set; }
public Nullable<System.DateTime> DataAdm { get; set; }
public bool Pendencias { get; set; }
public string DescPendencia { get; set; }
public string Obs { get; set; }
public string PessoaFJ { get; set; }
public string NomeFantasia { get; set; }
public string CodDep { get; set; }
public string AtivoInativo { get; set; }
public bool Ativado { get; set; }
public Nullable<int> IdSitBloq { get; set; }
public Nullable<int> SitBloq { get; set; }
public string Profissao { get; set; }
public string Empresa { get; set; }
public string NomeEsposa { get; set; }
public Nullable<System.DateTime> NascEsposa { get; set; }
public Nullable<int> IdNaturezaPadrao { get; set; }
public Nullable<int> ViaCarteirinha { get; set; }
public Nullable<int> Casa { get; set; }
public Nullable<int> Renda { get; set; }
public Nullable<int> RendaComplementar { get; set; }
public string Naturalidade { get; set; }
public string Banco { get; set; }
public string Agencia { get; set; }
public string CidadeBanco { get; set; }
public bool VeiculoProprio { get; set; }
public Nullable<System.DateTime> DIB { get; set; }
public string Foto { get; set; }
public string Nbeneficio { get; set; }
public Nullable<bool> LiberarExame { get; set; }
public Nullable<System.DateTime> ValidadeExame { get; set; }
public Nullable<int> EnviaBoleto { get; set; }
public Nullable<int> IdUsuario { get; set; }
public Nullable<long> IdClienteGlobal { get; set; }
public virtual IList<Clt_cadContatos> Clt_cadContatos { get; set; }
}
第2类:
public partial class Clt_cadContatos
{
public int IdContato { get; set; }
public string Nome { get; set; }
public string Telefone { get; set; }
public string Email { get; set; }
public bool AdicionarLista { get; set; }
public Nullable<int> IdCliente { get; set; }
}
当然,我不能测试它,但我有一个类似的情况,如果您不插入表Clt_cadContatos的字段,特别是该表的字段IdCliente的名称,那么库无法解析splitOn参数
var lookup = new Dictionary<int, Clt_cadCliente>();
cn.Query<Clt_cadCliente, Clt_cadContatos, Clt_cadCliente>(@"
SELECT s.*, a.IdCliente, a.OtherField1, a.OtherField2, etc ....
FROM Clt_cadCliente s
INNER JOIN Clt_cadContatos a ON s.IdCLiente = a.IdCliente ", (s, a) =>
{
Clt_cadCliente shop;
if (!lookup.TryGetValue(s.IdCliente, out shop))
{
lookup.Add(s.IdCliente, shop = s);
}
shop.Clt_cadContatos.Add(a);
return shop;
}, splitOn: "IdCliente").AsQueryable();
var resultList = lookup.Values;
您使用的参数错误。您需要在IdContato上拆分
split on字段告诉Dapper一个实体的结束位置和下一个实体的开始位置。如果选择s.*后跟a.*,则希望在表a的第一个字段上拆分为第二个实体,我假设sql表与类相似。是否尝试过此解决方案?