C# 从具体化的';系统。双';键入';系统。单个';类型无效。我能解决这个错误吗
我的项目中有一个DTO类。因此,当我在运行时运行时,我有以下消息: 从物化的“System.Double”类型到 “System.Single”类型无效 仅当我输入float或double类型时,才会显示此消息。我将DTO映射到类,但错误仍在继续。使用十进制或整数可以很好地工作。这是我的DTO课程:C# 从具体化的';系统。双';键入';系统。单个';类型无效。我能解决这个错误吗,c#,C#,我的项目中有一个DTO类。因此,当我在运行时运行时,我有以下消息: 从物化的“System.Double”类型到 “System.Single”类型无效 仅当我输入float或double类型时,才会显示此消息。我将DTO映射到类,但错误仍在继续。使用十进制或整数可以很好地工作。这是我的DTO课程: public class LiberacaoItensDTO { public LiberacaoItensDTO() { Mappe
public class LiberacaoItensDTO
{
public LiberacaoItensDTO()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
.ForMember(d => d.DataLib, opt => opt.MapFrom(src => Convert.ToDateTime(DataLib)));
cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
.ForMember(d => d.Juros, opt => opt.MapFrom(src => Juros.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
.ForMember(d => d.Desconto, opt => opt.MapFrom(src => Desconto.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
.ForMember(d => d.Acrescimo, opt => opt.MapFrom(src => Acrescimo.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
.ForMember(d => d.Vencimento, opt => opt.MapFrom(src => Convert.ToDateTime(Vencimento)));
cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
.ForMember(d => d.Entrada, opt => opt.MapFrom(src => Entrada.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
.ForMember(d => d.Qtde, opt => opt.MapFrom(src => Qtde.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
.ForMember(d => d.Unitario, opt => opt.MapFrom(src => Unitario.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
.ForMember(d => d.Custo, opt => opt.MapFrom(src => Custo.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
.ForMember(d => d.CustoDiario, opt => opt.MapFrom(src => CustoDiario.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
.ForMember(d => d.UltCondicao, opt => opt.MapFrom(src => UltCondicao.ToString("C2")));
cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
.ForMember(d => d.Total, opt => opt.MapFrom(src => Total.ToString("C2")));
});
}
public int IdLiberacao { get; set; }
[DefaultValue(0)]
public int IdOrcamento { get; set; }
[DefaultValue(0)]
public int IdVendedor { get; set; }
public string Vendedor { get; set; }
public int IdFilial { get; set; }
public string Filial { get; set; }
[DefaultValue(0)]
public float DataLib { get; set; }
public int IdCliente { get; set; }
public string Cliente { get; set; }
public string TipoVenda { get; set; }
[DefaultValue(0)]
public float Juros { get; set; }
[DefaultValue(0)]
public float Desconto { get; set; }
[DefaultValue(0)]
public float Vencimento { get; set; }
[DefaultValue(0)]
public float Acrescimo { get; set; }
[DefaultValue(0)]
public float Entrada { get; set; }
public string Mensagem { get; set; }
public int IdProduto { get; set; }
public string Produto { get; set; }
[DefaultValue(0)]
public float Qtde { get; set; }
[DefaultValue(0)]
public float Unitario { get; set; }
[DefaultValue(0)]
public float Custo { get; set; }
[DefaultValue(0)]
public float CustoDiario { get; set; }
[DefaultValue(0)]
public float UltCondicao { get; set; }
[DefaultValue(0)]
public float Total { get; set; }
}
公共类LiberacaoItensDTO
{
公共自由
{
Mapper.Initialize(cfg=>
{
cfg.CreateMap()
.ForMember(d=>d.DataLib,opt=>opt.MapFrom(src=>Convert.ToDateTime(DataLib));
cfg.CreateMap()
.ForMember(d=>d.Juros,opt=>opt.MapFrom(src=>Juros.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.descon,opt=>opt.MapFrom(src=>descon.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.Acrescimo,opt=>opt.MapFrom(src=>Acrescimo.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.Vencimento,opt=>opt.MapFrom(src=>Convert.ToDateTime(Vencimento));
cfg.CreateMap()
.ForMember(d=>d.Entrada,opt=>opt.MapFrom(src=>Entrada.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.Qtde,opt=>opt.MapFrom(src=>Qtde.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.Unitario,opt=>opt.MapFrom(src=>Unitario.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.Custo,opt=>opt.MapFrom(src=>Custo.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.CustoDiario,opt=>opt.MapFrom(src=>CustoDiario.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.ultcondico,opt=>opt.MapFrom(src=>ultcondico.ToString(“C2”));
cfg.CreateMap()
.ForMember(d=>d.Total,opt=>opt.MapFrom(src=>Total.ToString(“C2”));
});
}
public int idliberaco{get;set;}
[默认值(0)]
公共int IdOrcamento{get;set;}
[默认值(0)]
public int-IdVendedor{get;set;}
公共字符串卖方{get;set;}
公共int-IdFilial{get;set;}
公共字符串{get;set;}
[默认值(0)]
公共浮点数据库{get;set;}
public int IdCliente{get;set;}
公共字符串客户端{get;set;}
公共字符串TipoVenda{get;set;}
[默认值(0)]
公共浮动陪审团{get;set;}
[默认值(0)]
公共浮点描述{get;set;}
[默认值(0)]
公共浮点数Vencimento{get;set;}
[默认值(0)]
公共浮点数Acrescimo{get;set;}
[默认值(0)]
公共浮点数{get;set;}
公共字符串Mensagem{get;set;}
public int IdProduto{get;set;}
公共字符串Produto{get;set;}
[默认值(0)]
公共浮点Qtde{get;set;}
[默认值(0)]
公共浮点数集{get;set;}
[默认值(0)]
公共浮动客户{get;set;}
[默认值(0)]
公共浮动托管{get;set;}
[默认值(0)]
公共浮点UltCondico{get;set;}
[默认值(0)]
公共浮点总数{get;set;}
}
这是我的方法。此方法返回DTO对象类型:
public List<LiberacaoItensDTO> getAutoriza(int idorcamento)
{
var lista = contexto.Liberacoes
//.Where(lib => lib.IdOrcamento == idorcamento)
.Join(contexto.ItensLibs, lib => lib.IdOrcamento, itens => itens.IdOrcamento, (lib,itens) => new { lib, itens})
.Where(a => a.lib.IdOrcamento == a.itens.IdOrcamento && a.lib.IdOrcamento == idorcamento)
.Select(libera => new LiberacaoItensDTO
{
TipoVenda = libera.lib.TipoVenda,
IdOrcamento = libera.lib.IdOrcamento,
Juros = libera.lib.Juros != 0 ? libera.lib.Juros : 0,
//Entrada = libera.lib.Entrada != 0 ? libera.lib.Entrada : 0,
//Acrescimo = libera.lib.Acrescimo != 0 ? libera.lib.Acrescimo : 0,
//Desconto = libera.lib.Desconto != 0 ? libera.lib.Desconto : 0,
Mensagem = libera.lib.Mensagem,
//DataLib = libera.lib.DataLib != 0 ? libera.lib.DataLib : 0,
Qtde = libera.itens.Qtde != 0 ? libera.itens.Qtde : 0,
Vendedor = libera.lib.Vendedor,
Cliente = libera.lib.Cliente,
Filial = libera.lib.Filial
}).ToList();
return lista;
}
public List getAutoriza(intIDorCamento)
{
var lista=自由主义的语境
//.Where(lib=>lib.IdOrcamento==IdOrcamento)
.Join(contexto.ItensLibs,lib=>lib.IdOrcamento,itens=>itens.IdOrcamento,(lib,itens)=>new{lib,itens})
.其中(a=>a.lib.IdOrcamento==a.itens.IdOrcamento&&a.lib.IdOrcamento==IdOrcamento)
.选择(LIBREA=>新LIBREACAOITENSDTO
{
TipoVenda=libera.lib.TipoVenda,
IdOrcamento=libera.lib.IdOrcamento,
Juros=libera.lib.Juros!=0?libera.lib.Juros:0,
//Entrada=libera.lib.Entrada!=0?libera.lib.Entrada:0,
//Acrescimo=libera.lib.Acrescimo!=0?libera.lib.Acrescimo:0,
//descon=libera.lib.descon!=0?libera.lib.descon:0,
mensage=libera.lib.mensage,
//DataLib=libera.lib.DataLib!=0?libera.lib.DataLib:0,
Qtde=libera.itens.Qtde!=0?libera.itens.Qtde:0,
Vendedor=libera.lib.Vendedor,
Cliente=libera.lib.Cliente,
孝顺的
}).ToList();
返回列表a;
}
我的休息服务:
public class LiberacaoController : ApiController
{
AutorizadorContext contexto = new AutorizadorContext();
PedidoLiberacao liberacao = new PedidoLiberacao();
[AcceptVerbs("Get")]
public IEnumerable<LiberacaoItensDTO> getLiberacao()
{
return liberacao.getAutoriza(1000012093).AsEnumerable().ToList();
}
}
公共类控制器:ApiController
{
AutorizadorContext contexto=新的AutorizadorContext();
PedidoLiberacao liberacao=新PedidoLiberacao();
[接受动词(“Get”)]
公共IEnumerable getLiberacao()
{
返回liberacao.getAutoriza(1000012093.AsEnumerable().ToList();
}
}
如果我评论这行
Juros=libera.lib.Juros!=0 ? libera.lib.Juros:0,
或其他带有float字段的,工作正常。否则不会。我在DTO类中将所有float和double字段更改为string,在我的应用程序中,我再次更改为float和double。我也遇到了这个错误,我在类中有float字段(就像你的DTO类)
我有包含fl的sql表
public class ReportArchiveHistoryQueryModel
{
public DateTime ArchiveDate { get; set; }
public float Energy { get; set; }
}
CREATE PROCEDURE [dbo].[sp_ReportArchiveHistory]
@SmartCounterId VARCHAR(10)
,@StartDate SMALLDATETIME
,@EndDate SMALLDATETIME
AS
SELECT ArchiveDate,Energy FROM Archive
WHERE Archive.SmartCounterId=@SmartCounterId
AND Archive.ArchiveDate>=@StartDate
AND Archive.ArchiveDate<=@EndDate
public List<ReportArchiveHistoryQueryModel> ArchiveHistory(short smartCounterId, DateTime startDate,DateTime endDate)
{
var pSmartCounterId = new SqlParameter("SmartCounterId",smartCounterId);
var pStartDate = new SqlParameter("StartDate",startDate);
var pEndDate = new SqlParameter("EndDate ", endDate);
var result = this.Database.SqlQuery<ReportArchiveHistoryQueryModel>("exec dbo.sp_ReportArchiveHistory @SmartCounterId, @StartDate, @EndDate", pSmartCounterId, pStartDate, pEndDate);
return result.ToList();
}
CREATE PROCEDURE [dbo].[sp_ReportArchiveHistory]
@SmartCounterId VARCHAR(10)
,@StartDate SMALLDATETIME
,@EndDate SMALLDATETIME
AS
SELECT ArchiveDate,CONVERT(varchar(10), Energy )as Energy FROM Archive
WHERE Archive.SmartCounterId=@SmartCounterId
AND Archive.ArchiveDate>=@StartDate
AND Archive.ArchiveDate<=@EndDate
public class ReportArchiveHistoryQueryModel
{
public DateTime ArchiveDate { get; set; }
public string Energy { get; set; }
}
ReportArchiveHistoryBusinessModel reportArchiveHistoryBusinessModel=new ReportArchiveHistoryBusinessModel();
reportArchiveHistoryBusinessModel.ArchiveDate = item.ArchiveDate;
reportArchiveHistoryBusinessModel.Energy = float.Parse(item.Energy);
public class ReportArchiveHistoryQueryModel
{
public DateTime ArchiveDate { get; set; }
public double Energy { get; set; }
}