在LINQ中搜索日期后获得完整的月份报告

lb3vh1jj  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(165)

我需要根据一些条件生成一个完整的月报告。第一个条件是-我从用户那里取一个匿名日期,在检查所有条件后,它将生成完整的月报告。
我试图生成报告,但这是返回一个天明智的报告。一切都很好,除了月份。请帮助我这样做。

[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!");
   }
}
hjqgdpho

hjqgdpho1#

你必须为这个月做条件,你正在做的日期。所以这样做。

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

这里我假设idLcOpenDate来自相同的时区。
根据注解,LcOpenDateDateTime?,因此您需要这样做。
LcOpenDate.Value.MonthLcOpenDate.Value.Year

mbskvtky

mbskvtky2#

DateTime提供完整的日期和时间,类似于

2022-11-11 11:44:53 PM

当你在数据库中存储数据的时候,你可能会忽略时间,比如:

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

这是x.LcOpenDate == id的唯一工作方式。否则它只会返回打开日期与您提供的时间精确到毫秒的项目。请尝试以下操作:

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

此外,如果您使用的是.net 6+,则可以使用DateOnlyTimeOnly

相关问题