Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 221953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:04:55+00:00 2026-05-11T19:04:55+00:00

How should I handle a custom property in a situation where I use partial

  • 0

How should I handle a custom property in a situation where I use partial classes with the EF-generated classes?

Here’s the setup:

TimeSheet Table – this stores an employee’s hours
– TimeSheetID (auto, int, PK)
– EntryDate (DateTime)
– Hours (int)
– EmployeeID (int)

EmployeeHourlyRate table – this stores an employee’s current hourly rate. Historical rates are stored here as well.
– RateID (int, PK)
– EffectiveDate (int, PK)
– Rate (double)

This is a one to many relationship from TimeSheet to EmployeeHourlyRate. In order to find an Employee’s rate, I would select the max effectiveDate less than the timeSheet’s EntryDate.

In order to facilitate things, I’ve made a partial class called TimeSheet and added a new property called “Rate” to that class. What I would like to do is populate that myself from the same query that populates my collection of TimeSheets. I just know of no easy and clean way to handle this.

For example, I could do it this way:

var list = from ts in Context.TimeSheets
          ....
          select new TimeSheet() {
                 TimeSheetID = ts.TimeSheetID,
                 EntryDate = ts.EntryDate,
                 Hours = ts.Hours,
                 EmployeeID = ts.EmployeeID,
                 Rate = SomeRate   //real code has been omitted 
          };

This in theory should work, but for some reason the EF complains at run-time that I’m re-using an entity generated class (no idea why — it works fine if I create my own custom class). However, even if it did work, I’ve still got to maintain a list of fields and keep on mapping from my EF to a single class — i.e, maintenance becomes a problem when/if I add new fields to the TimeSheet table. It is also silly to have to re-type all that info.

So my question is, how do people generally handle this scenario? Is there a way to do something in the datamodel that would be able to effectively know my join rule (about selecting the correct effective date based on my EntryDate) and handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:04:55+00:00Added an answer on May 11, 2026 at 7:04 pm

    I would like to see your full LINQ query (including the ‘SomeRate’ code) to see exactly what you are trying to achieve, but maybe something like this could work:

    WARNING: Air code.

    public partial class TimeSheet
    {
        public double Rate
        {
            get //Calculate your rate here... e.g.
            {
                if ((this.Employee == null) || (this.Employee.EmployeeHourlyRates.Count == 0))
                    //throw an exception
    
                EmployeeHourlyRate maxRate;
                foreach (EmployeeHourlyRate rate in this.Employee.EmployeeHourlyRates)
                {
                    if ((rate.EffectiveDate <= this.EntryDate)
                        && ((maxRate == null) || (maxRate.EffectiveDate < rate.EffectiveDate)))
                    {
                        maxRate = rate;
                    }
                }
    
                if (maxRate == null)
                    //throw exception
                else
                    return maxRate.Rate;
            }
        }
    }
    

    EDIT: Adding example of eager loading to avoid database round trips.

    var list = from ts in Context.TimeSheets.Include("Employee.EmployeeHourlyRate")
               where blah blah
               select ts;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Given that all IDs end in "ActiveArea", you can do… May 13, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer @@identity is usually wrong in sql server as well. You… May 13, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Send the EM_SCROLLCARET message. Position the caret first, GetWindowTextLength() and… May 13, 2026 at 6:13 pm

Related Questions

I like to create an instance of a custom class by passing it a
I am struggling with the best way to handle a WPF application that has
I have a custom class Foo with properties A and B. I want to
I have been assigned a project to develop a set of classes that act

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.