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 8373805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:46:19+00:00 2026-06-09T14:46:19+00:00

In order to simplify creating fake data for unit testing, I want to have

  • 0

In order to simplify creating fake data for unit testing, I want to have a function that can create a generic Entity (which I use as an underlying object for a lot of other classes). The entity has an index, and, for generating a new index, I want to simply find the currently highest number and add one.

I am using Index for a lot of other classes, and would like to make this function as generic as possible. My problem is that I don’t know how to specify what DbSet to use in my generic function GetMaxID.

This is what I got so far:

private Entity CreateGenericEntity()
{
    return new Entity()
    {
        Guid = Guid.NewGuid(),
        Index = GetMaxID<Entity>(x => x.Index) + 1,
    };
}

private int GetMaxID<TEntity>(Expression<Func<TEntity, int>> expression)
{
    return _repository.Set<TEntity>().Max(expression);
}

_repository has a bunch of different IDbSets properties, such as

public IDbSet<Customers> Customers{ get; set; }

public IDbSet<Orders> Orders{ get; set; }

etc.

  • 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-09T14:46:20+00:00Added an answer on June 9, 2026 at 2:46 pm

    I found that I was missing the declaration of TEntity, which was fixed by appending where TEntity : class to my function. I also had to change the expression to accept int? in order to handle the case where the query returns a null value. The complete function looks like this (if anyone is interested)

    private int GetMaxID<TEntity>(Expression<Func<TEntity, int?>> expression) where TEntity : class
    {
        return _repository.Set<TEntity>().Max(expression) ?? 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a listing of data that has a randomly generated order listing.
I have simplified the code I have in order to simplify, but here it
I have a node form in Drupal 7, in order to simplify it for
I am busy creating an order system. I am having issues trying to create
Out of order execution in CPUs means that a CPU can reorder instructions to
I am busy creating an order system. I am having issues trying to create
I have the following regex: (?i:^TPI$|^TIP$|^IPT$|^ITP$|^PIT$|^PTI$|^IP$|^PI$|^TI$|^IT$|^PT$|^TP$|^T$|^P$|^I$) How can I simplify it? My regular expression
I am creating a class (Datamap) that will help me simplify my queries in
Now I got a TabHost with several tabs, and in order to simplify the
Order by descending is not working on LINQ to Entity In the following Query

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.