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

  • SEARCH
  • Home
  • 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 8904873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:59+00:00 2026-06-15T02:14:59+00:00

I have implemented in the Add() method of my generic EF repository a check

  • 0

I have implemented in the Add() method of my generic EF repository a check whether the row I’m going to insert already exists in the table, if it does, update it with the currently available info.

private List<T> _previousEntries;

//Try and find one with same PK in the temporary tables...
var previousRow = _previousEntries.Single(n => (int)n.GetType().GetProperty(_PKName).GetValue(n, null) == entPKValue);

//If we did find one...
if (previousRow != null)
{
   //Update the row...
   return;
}

//Add it...

So I know, I’m using reflection, which is slow but I have not found another way since different entities have different SQL PK names.

But I’m not sure that reflection is the biggest issue here, sometimes, _previousEntries will hold up to 800,000 items.

_previousEntries has its items assigned to it in the class constructor of the repository class. _PKName is also assigned a value in the class constructor depending on the type of T.

If I just set a breakpoint on the Single() statement, it can be processing for 2-3 seconds for sure so I don’t know how I could determine what is the bottleneck here: reflection or Single() on 800,000 items… It sure goes way faster on a 5,000 items list.

Any opinions ? Is there anything I can do to optimize my List ?

  • 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-06-15T02:15:00+00:00Added an answer on June 15, 2026 at 2:15 am

    Provide a primary key accessor as a delegate

    public class Repository<T>
    {
        private Funct<T,int> _getPK;
        private Dictionary<int,T> _previousEntries;
    
        public Repository(Funct<T,int> getPK)
        {
            _getPK = getPK;
            _previousEntries = new Dictionary<int,T>();
        }
    
        public void Add(T item) {
            ...
            int pk = _getPK(item);
            T previousEntry;
            if (_previousEntries.TryGetValue(pk, out previousEntry) {
                // Update
            } else {
                // Add
                _previousEntries.Add(pk, item);
            }
        }
    }
    

    You would create a repositiory like this

    var clientRepository = new Repositiory<Client>(c => c.ClientID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have implemented this method to add a footer to my table view:
I have a generic repository an I am trying to add a GetById method
I have generic repository. There is a method Save() to persist entity in db.
I have a generic repository and would like to implement a generic GetById method.
I have the following generic method inside my class that works as a repository
I have implemented a table view with multiple threads. Currently when a user taps
I have implemented a test method with Jersey to run on my Google AppEngine
I have implemented a UISearchDisplayController that allows users to search a table. Currently the
I have a generic method something like: public abstract T method<T>(int arg) where T
I have read a number of posts regarding the implementation of a generic repository.

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.