Why is my code not returning any records? Even though there are rows found. (Entity Framework Core)

using Binus.WS.Pattern.Entities;
using Microsoft.EntityFrameworkCore.Metadata;
using SSG5.C1.BSQWebAPI.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Extensions.Logging;

namespace SSG5.C1.BSQWebAPI.Helper
{
    public class ElectricHistoryHelper
    {
        private static readonly ILogger<ElectricHistoryHelper> _logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<ElectricHistoryHelper>();

        public static List<ElectricHistory> GetElectricHistory(ElectricHistory model, string binusianId)
        {
            // Fetch the active period ID
            var PeriodActive = EntityHelper.Get<MsPeriod>(t => t.AuditedActivity != "D" && t.ActivePeriod == true)
                                           .Select(t => t.PeriodId)
                                           .FirstOrDefault();

            _logger.LogInformation($"Active Period ID: {PeriodActive}");

            using (var context = new EntityContext<ElectricHistory, TrRoomAllocation, TrBoarderRegistration>())
            {
                DateTime? startDate = DateTime.ParseExact(model.StartDate, "MM-dd-yyyy", CultureInfo.InvariantCulture);
                DateTime? endDate = DateTime.ParseExact(model.EndDate, "MM-dd-yyyy", CultureInfo.InvariantCulture);

                _logger.LogInformation($"Fetching data for BinusianId: {binusianId}, StartDate: {startDate}, EndDate: {endDate}");

                // Fetch data without transformations
                var electricHistories = context.Set<ElectricHistory>()
                    .Where(e => e.Tanggal >= startDate && e.Tanggal <= endDate)
                    .ToList();

                _logger.LogInformation($"Fetched {electricHistories.Count} electricHistories");

                var roomAllocations = context.Set<TrRoomAllocation>()
                    .Where(r => r.AuditedActivity != "D" && r.PeriodId == PeriodActive)
                    .ToList();

                _logger.LogInformation($"Fetched {roomAllocations.Count} roomAllocations");

                var boarderRegistrations = context.Set<TrBoarderRegistration>()
                    .Where(b => b.BinusianId == binusianId)
                    .ToList();

                _logger.LogInformation($"Fetched {boarderRegistrations.Count} boarderRegistrations");

                // Log room allocations and boarder registrations
                foreach (var r in roomAllocations)
                {
                    _logger.LogInformation($"RoomAllocation - RoomNo: {r.RoomNo}, RegistrationId: {r.RegistrationId}");
                }

                foreach (var b in boarderRegistrations)
                {
                    _logger.LogInformation($"BoarderRegistration - BinusianId: {b.BinusianId}, RegistrationId: {b.RegistrationId}");
                }

                // Perform the join and transformation in memory
                var result = (from e in electricHistories
                              let transformedKete = e.Kete.Substring(e.Kete.Length - 7).Replace("-", ".")
                              join r in roomAllocations on transformedKete equals r.RoomNo
                              join b in boarderRegistrations on r.RegistrationId equals b.RegistrationId
                              orderby e.Tanggal descending
                              select new
                              {
                                  ElectricHistory = e,
                                  TransformedKete = transformedKete,
                                  RoomNo = r.RoomNo,
                                  RegistrationId = r.RegistrationId
                              })
                              .Skip((model.Page - 1) * model.PageSize)
                              .Take(model.PageSize)
                              .ToList();

                // okay so this part isn't returning because ganemu results trus berarti di result nya ada salah join atau ga, ga nemu apa2
                foreach (var item in result)
                {
                    _logger.LogInformation($"Matched Record - Kete: {item.ElectricHistory.Kete}, TransformedKete: {item.TransformedKete}, RoomNo: {item.RoomNo}, RegistrationId: {item.RegistrationId}");
                }

                _logger.LogInformation($"Retrieved {result.Count} records");

                return result.Select(item => item.ElectricHistory).ToList();
            }
        }

        public static int GetElectricHistoryCount(string binusianId, string startDate, string endDate)
        {
            var PeriodActive = EntityHelper.Get<MsPeriod>(t => t.AuditedActivity != "D" && t.ActivePeriod == true)
                                .Select(t => t.PeriodId)
                                .FirstOrDefault();

            _logger.LogInformation($"Active Period ID: {PeriodActive}");

            using (var context = new EntityContext<ElectricHistory, TrRoomAllocation, TrBoarderRegistration>())
            {
                DateTime? startDateParsed = DateTime.ParseExact(startDate, "MM-dd-yyyy", CultureInfo.InvariantCulture);
                DateTime? endDateParsed = DateTime.ParseExact(endDate, "MM-dd-yyyy", CultureInfo.InvariantCulture);

                _logger.LogInformation($"Counting data for BinusianId: {binusianId}, StartDate: {startDateParsed}, EndDate: {endDateParsed}");

                // Fetch data without transformations
                var electricHistories = context.Set<ElectricHistory>()
                    .Where(e => e.Tanggal >= startDateParsed && e.Tanggal <= endDateParsed)
                    .ToList();

                _logger.LogInformation($"Fetched {electricHistories.Count} electricHistories");

                var roomAllocations = context.Set<TrRoomAllocation>()
                    .Where(r => r.AuditedActivity != "D" && r.PeriodId == PeriodActive)
                    .ToList();

                _logger.LogInformation($"Fetched {roomAllocations.Count} roomAllocations");

                var boarderRegistrations = context.Set<TrBoarderRegistration>()
                    .Where(b => b.BinusianId == binusianId)
                    .ToList();

                _logger.LogInformation($"Fetched {boarderRegistrations.Count} boarderRegistrations");

                // Log room allocations and boarder registrations
                foreach (var r in roomAllocations)
                {
                    _logger.LogInformation($"RoomAllocation - RoomNo: {r.RoomNo}, RegistrationId: {r.RegistrationId}");
                }

                foreach (var b in boarderRegistrations)
                {
                    _logger.LogInformation($"BoarderRegistration - BinusianId: {b.BinusianId}, RegistrationId: {b.RegistrationId}");
                }

                // Perform the join and transformation in memory
                var count = (from e in electricHistories
                             let transformedKete = e.Kete.Substring(e.Kete.Length - 7).Replace("-", ".")
                             join r in roomAllocations on transformedKete equals r.RoomNo
                             join b in boarderRegistrations on r.RegistrationId equals b.RegistrationId
                             select e)
                             .Count();

                _logger.LogInformation($"Total records count: {count}");

                return count;
            }
        }
    }
}

This is my helper file that basically retrieves data from 3 tables, and joins it to get the desired columns, and returns it, but in the logging kept on logging 0 records.

My guess is somethings wrong when joining the tables, but I still don’t know how to fix it. Any help would be very much appreciated, thank you!

Initially I tried using the Substring and Replace LINQ functions directly in the LINQ to entities, but I tried asking GPT on an error before, and it said it was because using LINQ functions like those directly in the LINQ to Entities won’t work because it isn’t supported and it suggested to do the operations in memory, then it can work, but this is the wall that I’ve been hitting for the last 2 days.

foreach (var item in result)
{
    _logger.LogInformation($"Matched Record - Kete: {item.ElectricHistory.Kete}, TransformedKete: {item.TransformedKete}, RoomNo: {item.RoomNo}, RegistrationId: {item.RegistrationId}");
}

This is the console logs if needed:

RoomAllocation - RoomNo: C.04.26, RegistrationId: 222e6978-de5f-4766-b55c-acfe298aba53
info: SSG5.C1.BSQWebAPI.Helper.ElectricHistoryHelper[0]

BoarderRegistration - BinusianId: BN123481406, RegistrationId: ec40a5bc-f735-485a-a67c-efff6a25dcf5
info: SSG5.C1.BSQWebAPI.Helper.ElectricHistoryHelper[0]

Retrieved 0 records

New contributor

JUVÉ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật