Mapping Functions EF Core to SQL

I’m looking for a more efficient way to grab my objects from the database and map them DTOs while using reusable methods.

Here are my database classes

    public class Sign
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
    
        public int? SignGroupId { get; set; }
        public SignGroup SignGroup { get; set; } = null!;

        public int LocationId { get; set; }
        public Location Location { get; set; } = null!;

        public static Expression<Func<Sign, SignDTO>> MapToDto()
        {
            return s => new()
            {
                Id = s.Id,
                Name = s.Name ?? "",

    //this call works as I would expect by translating into SQL and executing it at the
    //database level.
                Signs = s.SignGroup.Signs.Where(ss => ss.Id != s.Id)
                    .AsQueryable().Select(MapToGroupSignDto()).ToList(),

    //This call will end up bringing the whole Location object into memory and then
    //perform the mapping from memory.
                LocationInfo = Location.MapToSignLocationDto()
                    .Compile().Invoke(s.Location),
            };
        }

        public static Expression<Func<Sign, GroupSignDTO>> MapToGroupSignDto()
        {
            return gs => new()
            {
                Id = gs.Id,
                Name = gs.Name ?? "",
            };
        }
    }
    
    public class SignGroup
    {
        public int Id { get; set; }
        public List<Sing> Signs { get; set; } = null!;
    }
    
    public class Location
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
        public string? Latitude { get; set; }
        public string? Longitude { get; set; }
        public string? State { get; set; }
        public string? City { get; set; }
        public string? Zip { get; set; }
        public string? StreetAddress { get; set; }
        
        public static Expression<Func<Location, SignLocationDTO>> MapToSignLocationDto()
        {
            return l => new()
            {
                Id = l.Id,
                Name = l.Name,
                Latitude = l.Latitude ?? "",
                Longitude = l.Longitude ?? "",
            };
        }
    }

Here are my DTO Objects:

 public record SignDTO
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
    
        public int SignGroupId { get; set; }
        [Comment("Signs At Same Location")]
        public List<GroupSignDTO> AdditionalSings { get; set; } = null!;
    
        public SignLocationDTO LocationInfo { get; set; } = null!;
    }
    
    public record GroupSignDTO
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
    }
    
    public record LocationDTO
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
        public string Latitude { get; set; } = null!;
        public string Longitude { get; set; } = null!;
    }

Here are my series of functions calls as they exist now. I know the expression functions are repeated in their above classes, but I wanted to make it easier to follow.

public async Task<List<SignDTO>> GetListDataAsync()
    {
        IQueryable<Sign> signIQ = _context.Signs;
        Queryable<SignDTO> dtoIQ = signIQ.Select(Sign.MapToDto());
    
        return await dtoIQ.ToListAsync();
    }
    
    public static Expression<Func<Sign, SignDTO>> MapToDto()
    {
        return s => new()
        {
            Id = s.Id,
            Name = s.Name ?? "",
    
    //this call works as I would expect by translating into SQL and executing it at the
    //database level.
            Signs = s.SignGroup.Signs.Where(ss => ss.Id != s.Id)
                .AsQueryable().Select(MapToGroupSignDto()).ToList(),
    
    //This call will end up bringing the whole Location object into memory and then
    //perform the mapping from memory.
            LocationInfo = Location.MapToSignLocationDto()
                .Compile().Invoke(s.Location),
        };
    }
    
    public static Expression<Func<Sign, GroupSignDTO>> MapToGroupSignDto()
    {
        return gs => new()
        {
            Id = gs.Id,
            Name = gs.Name ?? "",
        };
    }
    
    public static Expression<Func<Location, SignLocationDTO>> MapToSignLocationDto()
    {
        return l => new()
        {
            Id = l.Id,
            Name = l.Name,
            Latitude = l.Latitude ?? "",
            Longitude = l.Longitude ?? "",
        };
    }

What I’m hoping to find, if possible, is a simple but also better way to grab the LocationInfo that would put the whole call into SQL and allow the database to only return what I really need without causing the SQL to take for ever to execute. I don’t like the idea of the database returning all the info related to the Location and then the server using memory to pair it down to what I really need.

I have tried various different ways to do this, but the only way that has accomplished what I wanted was to put all the code into a single Expression function like:

public static Expression<Func<Sign, SignDTO>> MapToDto()
    {
        return s => new()
        {
            Id = s.Id,
            Name = s.Name ?? "",

            Signs = s.SignGroup.Signs.Where(ss => ss.Id != s.Id)
                .Select(gs => new()
                {
                    Id = gs.Id,
                    Name = gs.Name ?? "",
                ).ToList(),

            LocationInfo = new()
            {
                Id = s.Location.Id,
                Name = s.Location.Name,
                Latitude = s.Location.Latitude ?? "",
                Longitude = s.Location.Longitude ?? "",
            },
        };
    }

My issue with this is that the code is not reusable in other areas and in order to maintain any future changes I’d have to go to multiple places.

Any help and ideas in this area would be greatly appreciated.
Thank you 🙂

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