简体   繁体   English

无法隐式转换类型'System.DateTime?' 到'System.DateTime'

[英]Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'

I'm trying to assign the value to dtLancamentoReceitaDespesaDataVencimento and the condition is: if value dtLancamentoReceitaDespesaDataPagamento is null assigns the value dtLancamentoReceitaDespesaDataVencimento but assigns the value of dtLancamentoReceitaDespesaDataPagamento. 我正在尝试将值赋给dtLancamentoReceitaDespesaDataVencimento,条件是:如果值dtLancamentoReceitaDespesaDataPagamento为null,则赋值dtLancamentoReceitaDespesaDataVencimento但指定dtLancamentoReceitaDespesaDataPagamento的值。 But is giving the following error: 但是给出了以下错误:

Can not implicitly convert type 'System.DateTime?' 无法隐式转换类型'System.DateTime?' to 'System.DateTime'. 到'System.DateTime'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否错过了演员?)

dtLancamentoReceitaDespesaDataVencimento = 
   lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento == null ? 
   lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataVencimento : 
   lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento     

LINQ: LINQ:

    public List<LancamentoReceitaDespesa> GetLancamentoReceitaDespesaByintCodigoGrupoUsuarioByParametro(int intCodigoGrupoUsuario, int mes, int ano)
    {
        return (from lancamentoReceitaDespesa in _DatabaseContext.LancamentoReceitaDespesa
                where lancamentoReceitaDespesa.intCodigoGrupoUsuario == intCodigoGrupoUsuario &&
                lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataVencimento.Month == mes &&
                lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataVencimento.Year == ano
                select new LancamentoReceitaDespesa
                {
                    intLancamentoReceitaDespesaId = lancamentoReceitaDespesa.intLancamentoReceitaDespesaId,
                    intCategoriaId = lancamentoReceitaDespesa.intCategoriaId,
                    strLancamentoReceitaDespesaCartaoCreditoContaBancaria = lancamentoReceitaDespesa.strLancamentoReceitaDespesaCartaoCreditoContaBancaria,
                    intContaContabilId = lancamentoReceitaDespesa.intContaContabilId,
                    intFormaPagamentoId = lancamentoReceitaDespesa.intFormaPagamentoId,
                    intClienteFornecedorId = lancamentoReceitaDespesa.intClienteFornecedorId,
                    intEventoId = lancamentoReceitaDespesa.intEventoId,
                    strLancamentoReceitaDespesaDescricao = lancamentoReceitaDespesa.strLancamentoReceitaDespesaDescricao,
                    strLancamentoReceitaDespesaDocumento = lancamentoReceitaDespesa.strLancamentoReceitaDespesaDocumento,
                    strLancamentoReceitaDespesaTipoLancamento = lancamentoReceitaDespesa.strLancamentoReceitaDespesaTipoLancamento,
                    dtLancamentoReceitaDespesaDataVencimento = lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento == null ? lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataVencimento : lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento,
                    decLancamentoReceitaDespesaValor = lancamentoReceitaDespesa.decLancamentoReceitaDespesaValor,
                    dtLancamentoReceitaDespesaDataPagamento = lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento,
                    decLancamentoReceitaDespesaValorPago = lancamentoReceitaDespesa.decLancamentoReceitaDespesaValorPago,
                    dtLancamentoReceitaDespesaDataRecebimento = lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataRecebimento,
                    decLancamentoReceitaDespesaValorRecebido = lancamentoReceitaDespesa.decLancamentoReceitaDespesaValorRecebido,
                    dtLancamentoReceitaDespesaDataCompra = lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataCompra,
                    intCodigoGrupoUsuario = lancamentoReceitaDespesa.intCodigoGrupoUsuario,
                    bitLancamentoReceitaDespesaPagarAutomatico = lancamentoReceitaDespesa.bitLancamentoReceitaDespesaPagarAutomatico,
                    bitLancamentoReceitaDespesaLancamentoPago = lancamentoReceitaDespesa.bitLancamentoReceitaDespesaLancamentoPago,
                    dtLancamentoReceitaDespesaDataAlteracao = lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataAlteracao,
                    dtLancamentoReceitaDespesaDataCriacao = lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataCriacao
                }).ToList();

    }

If dtLancamentoReceitaDespesaDataVencimento is of DateTime type, then just add nullable type Value selector at the end: 如果dtLancamentoReceitaDespesaDataVencimentoDateTime类型,那么只需在末尾添加可空类型Value选择器:

dtLancamentoReceitaDespesaDataVencimento = 
  lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento == null ? 
  lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataVencimento : 
  lancamentoReceitaDespesa.dtLancamentoReceitaDespesaDataPagamento.Value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;System.DateTime&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.DateTime' to 'string' 无法将类型“object”隐式转换为“System.DateTime”。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?) 无法隐式转换类型&#39;System.DateTime?&#39; 到“ int?” C#Linq - Cannot implicitly convert type 'System.DateTime?' to 'int?' C# Linq 无法从“ System.DateTime输出?”中进行转换? 退出System.DateTime - cannot convert from 'out System.DateTime?' to 'out System.DateTime 无法将System.String转换为System.DateTime - Cannot convert System.String to System.DateTime null 值不能分配给 System.DateTime 类型的成员,该类型是不可为空的值类型 - The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type 无法将DBNull.Value强制转换为“System.DateTime”。 请使用可空类型 - Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type SQL Server的DateTime与System.DateTime - DateTime sql server vs. System.DateTime Datecolumn:无法将“System.DateTime”类型的对象强制转换为“System.String”类型 - Datecolumn: Unable to cast object of type 'System.DateTime' to type 'System.String' Linq2Sql中的System.DateTime和Sql datetime2(使用sqlmetal) - System.DateTime and Sql datetime2 in Linq2Sql (using sqlmetal)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM