简体   繁体   English

C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate”

[英]C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate'

I have this model in my ASP.NET Core Web API:我的 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D083A1 中有这个 model:

Models:楷模:

namespace Core.Models
{
  public class Mandate : EntityBase
  {
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? PaymentFrequency { get; set; }  //1=Monthly, 2=Quarterly, 3=Yearly
    public int? PaymentCount { get; set; }
    public decimal Amount { get; set; }
  }
}

ViewModel:视图模型:

public class MandateVM
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? PaymentFrequency { get; set; }  //1=Monthly, 2=Quarterly, 3=Yearly
    public decimal Amount { get; set; }
}

I have written some helpers that gets T otalMonth , TotalQuarter and TotalYear from StartDate and EndDate as show below:我编写了一些从StartDate 和 EndDate获取TotalMonthTotalQuarterTotalYear助手,如下所示:

namespace Infrastructure.Helpers
{
  public static class ConstantHelper
  {
    private static IConfiguration Configuration { get; set; }

    public static int GetTotalMonth(DateTime startDate, DateTime endDate)
    {
        int totalMonth = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
        return Convert.ToInt32(Math.Abs(totalMonth));
    }

    public static int GetTotalQuarter(DateTime startDate, DateTime endDate)
    {
        int firstQuarter = getQuarter(startDate);
        int secondQuarter = getQuarter(endDate);
        return 1 + Math.Abs(firstQuarter - secondQuarter);
    }

    private static int getQuarter(DateTime date)
    {
        return (date.Year * 4) + ((date.Month - 1) / 3);
    }

    public static int GetTotalYear(DateTime startDate, DateTime endDate)
    {
        int years = endDate.Year - startDate.Year;

        if (startDate.Month == endDate.Month &&// if the start month and the end month are the same
            endDate.Day < startDate.Day)// BUT the end day is less than the start day
        {
            years--;
        }
        else if (endDate.Month < startDate.Month)// if the end month is less than the start month
        {
            years--;
        }
        return Math.Abs(years);
    }
  }
}

EntityMapper:实体映射器:

    public Mandate FromMandateCreateDtoToMandate(MandateVM mandateCreateVM)
    {
        if (mandateCreateVM == null)
        {
            return null;
        }

        Mandate mandate = new Mandate()
        {
            StartDate = mandateCreateVM.StartDate,
            EndDate = mandateCreateVM.EndDate,
            DueDate = mandateCreateVM.DueDate,
            PaymentFrequency = mandateCreateVM.PaymentFrequency, //1=Monthly, 2=Quarterly, 3=Yearly
            Amount = mandateCreateVM.Amount,
        };
        int numberOfTimes;
        switch (mandateCreateVM.PaymentFrequency)
        {
            case 1:
                return numberOfTimes = ConstantHelper.GetTotalMonth(mandate.StartDate, mandate.EndDate);
                break;
            case 2:
                return numberOfTimes = ConstantHelper.GetTotalQuarter(mandate.StartDate, mandate.EndDate);
                break;
            default:
                return numberOfTimes = ConstantHelper.GetTotalYear(mandate.StartDate, mandate.EndDate);
                break;
        }
        mandate.PaymentCount = numberOfTimes;
        return mandate;         
    }

PaymentFrequency determines number of times (numberOfTimes) payment will be made. PaymentFrequency确定付款的次数(numberOfTimes)

1 = Monthly 1 = 每月

2 = Quarterly 2 = 每季度

3 = Yearly 3 = 每年

I expected it to return numberOfTimes as int, but I got this error:我希望它以 int 形式返回 numberOfTimes,但我收到了这个错误:

Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate'错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate”

It highlights:它强调:

return numberOfTimes = ConstantHelper.GetTotalMonth(mandate.StartDate, mandate.EndDate);返回 numberOfTimes = ConstantHelper.GetTotalMonth(mandate.StartDate, task.EndDate);

return numberOfTimes = ConstantHelper.GetTotalQuarter(mandate.StartDate, mandate.EndDate);返回 numberOfTimes = ConstantHelper.GetTotalQuarter(mandate.StartDate, task.EndDate);

return numberOfTimes = ConstantHelper.GetTotalYear(mandate.StartDate, mandate.EndDate);返回 numberOfTimes = ConstantHelper.GetTotalYear(mandate.StartDate, task.EndDate);

How do I get this resolved?我该如何解决这个问题?

Thanks谢谢

Just delete the return in every cases of your switch case in FromMandateCreateDtoToMandate() .只需在FromMandateCreateDtoToMandate()中删除每个 switch 案例中的return

Those return will try to exit the function and return numberOfTimes ( int ) instead of, I presume, as you wanted only exit the switch case那些return将尝试退出 function 并返回 numberOfTimes ( int )而不是,我想,因为你只想退出开关盒

暂无
暂无

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

相关问题 CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识 - CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' CS0029 C#无法将类型隐式转换为&#39;string []&#39; - CS0029 C# Cannot implicitly convert type to 'string[]' 我得到了 CS0029 C# 无法将类型“int”隐式转换为“bool” - i got CS0029 C# Cannot implicitly convert type 'int' to 'bool' C#错误:无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.Control&#39;(CS0029) - c# Error: Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029) 收到错误消息:CS0029:无法隐式转换(ASP.NET Core MVC 和 C#) - Getting error message: CS0029: Cannot implicitly convert (ASP.NET Core MVC & C#) 错误 CS0029:无法将类型“bool”隐式转换为“(bool valorA, bool valorB)”c# - error CS0029: Cannot implicitly convert type 'bool' to '(bool valorA, bool valorB)' c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM