Home > front end >  Getting a full month report after searching a date in LINQ
Getting a full month report after searching a date in LINQ

Time:11-12

I need to generate a full month report base on some criteria. The first condition is- I am taking a anonyms Date from user and after checking all the condition it will generate full month report.

I tried to generate the report but this is returning a day wise report. Everything is fine except the month. Please help me to do this.

[HttpGet("inner-join/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetReport(DateTime id)
{
  try
  {
    IEnumerable<BTBPending> objBTBPendingList = _unitOfWork.BTBPending.GetAll(includeProperties: "ProformaInvoice,ContractList,SupplierList,CountryList,ItemList,BuyerList,StyleList,TradeTermList,ErpRemarksList,StatusList,LcNoList,UdAmendList");

    IEnumerable<ProformaInvoice> objProformaInvoiceList = _unitOfWork.ProformaInvoice.GetAll(includeProperties: "ActualContract,ContractList,SupplierList,CountryList,ItemList,BuyerList,StyleList,TradeTermList");


    var query = objBTBPendingList
        .Where(x => x.LcOpenDate == id)
        .Where(x => x.CountryListId == 26)
        .Where(x => x.StatusListId == 12 || x.StatusListId == 13 || x.StatusListId == 14)
        .Join(objProformaInvoiceList,
              btbPending => btbPending.ContractListId,
              pi => pi.ContractListId, 
              (btbPending, pi) => new
              {
                LcNo = btbPending.LcNoList,
                Value = btbPending.PiValue,

                ContractNo = pi.ContractList,
                Buyer = pi.BuyerList,
                PiNo = pi.PINo,
                Supplier = pi.SupplierList,
                Item = pi.ItemList
              }).ToList();


     return Ok(query);
   }
 catch (Exception ex)
   {
     return StatusCode(500, "Internal Server Error, Please Try Again Leter!");
   }
}

CodePudding user response:

DateTime gives your a full date and time and looks something like

2022-11-11 11:44:53 PM

What is likely going on is that when you are storing the data in the database, you are ommitting the time. Something like this:

var date = new DateTime(now.Year, now.Month, now.Day) 
// 2022-11-11 12:00:00 AM

This is the only way x.LcOpenDate == id would work. Otherwise it would only return items with the open date at the exact time you provided up to the millisecond. Try this instead:

.Where(x => x.LcOpenDate == id.Year &&  x.LcOpenDate == id.Month)

Additionally, if you are on .net 6 you can use DateOnly and TimeOnly

CodePudding user response:

You have to make condition for the month, you are doing on the date. so do like this.

.Where(x => x.LcOpenDate.Month == id.Month && x.LcOpenDate.Year == id.Year)

Here I am assuming both id and LcOpenDate are from the same timezones.

  • Related