Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
<pre><big><pre>First in the database for time, I chose Big Int, but then I changed it to Small Data Time.


public async Task<IActionResult> Create(int? id, int? type)
{
    if (id == null || type == null)
    {
        return NotFound();
    }

    InsuranceViewModel model = null;
    ViewBag.Type = type;

    var companies = await _context.InsuranceCompany
        .Where(e => e.IsActice)
        .ToListAsync();
    ViewData["CompanyList"] = new SelectList(companies, "Id", "CompanyName");

    if ((InsuranceType)type == InsuranceType.Body)
    {
        var lastBody = _context.BodyInsurance
                       .Where(e => e.VehicleInformationId == id.Value)
                       .AsEnumerable()
                       .LastOrDefault();

        model = new InsuranceViewModel
        {
            InsuranceType = InsuranceType.Body,
            InsuranceNumber = "",
            IssueDate = lastBody != null
                ? new DateTime(lastBody.ExpireDate).AddDays(1)
                : DateTime.Today,
            ExpireDate = lastBody != null
                ? new DateTime(lastBody.ExpireDate).AddYears(1)
                : DateTime.Today.AddYears(1),
            VehicleInformationId = id.Value
        };
    }
    else if ((InsuranceType)type == InsuranceType.Thirdpart)
    {
        var lastThirdParty = _context.ThirdPartyInsurance
                             .Where(e => e.VehicleInformationId == id.Value)
                             .AsEnumerable()
                             .LastOrDefault();

        var thirdParty = await _context.ThirdPartyInsurance
        .Include(e => e.InsuranceCompany)
        .FirstOrDefaultAsync(e => e.Id == id.Value);

        model = new InsuranceViewModel
        {
            InsuranceType = InsuranceType.Thirdpart,
            InsuranceNumber  = "",
            IssueDate = lastThirdParty != null
                ? new DateTime(lastThirdParty.ExpireDate).AddDays(1)
                : DateTime.Today.Ticks,
            ExpireDate = lastThirdParty != null
                ? new DateTime(lastThirdParty.ExpireDate).AddYears(1)
                : DateTime.Today.AddYears(1),
            VehicleInformationId = id.Value
        };
    }

    return View(model);
}


What I have tried:

var lastThirdParty = await _context.ThirdPartyInsurance.Include(e => e.InsuranceCompany)
    .Where(e => e.VehicleInformationId == id)
    .OrderBy(e => e.ExpireDate)
    .LastOrDefaultAsync();

var lastBody = await _context.BodyInsurance.Include(e => e.InsuranceCompany)
    .Where(e => e.VehicleInformationId == id)
    .OrderBy(e => e.ExpireDate)
    .LastOrDefaultAsync();



The error is on this line:
? new DateTime(lastBody.ExpireDate).AddDays(1)
lastBody.ExpireDate ----- cannot convert from system.datetime to long
Posted
Updated 3-Jul-20 21:55pm
v4

1 solution

We have no idea what type lastBody.ExpireDate is, but from the error message and the property name I'd assume it's a DateTime - and there is understandably no DateTime constructor that takes a DateTime value as it's parameter: where would be the point given that the DateTime struct is immutable?

So it tries for a constructor overload that takes a single parameter - there is only one - and looks for an implicit cast that converts a DateTime to a long and again finds there isn't one, so it reports a compiler error.

Probably what you need to do is get rid of the new DateTime bit:
lastThirdParty.ExpireDate.AddDays(1)
Should do it, except ... the conditional operator needs both sides to be the same type, and the other side is a Ticks count.
Check what type IssueDate is and match both to that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900