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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:20:47+00:00 2026-05-18T05:20:47+00:00

I’m converting from LINQ to SQL to Entity Framework for my ORM and I’m

  • 0

I’m converting from LINQ to SQL to Entity Framework for my ORM and I’m updating my repositories. All of them are done except for the generic one. I can’t seem to figure out how to convert my Select method from what it is now to one that works with EF. Here’s the current code:

public T Select(int Id)
{
    MetaDataMember PrimaryKey = this.DataContext.Mapping.GetTable(typeof(T)).RowType.DataMembers.SingleOrDefault(
        d =>
            (d.IsPrimaryKey));
    ParameterExpression Param = Expression.Parameter(typeof(T), "e");

    var Query = Expression.Lambda<Func<T, bool>>(Expression.Equal(Expression.Property(Param, PrimaryKey.Name), Expression.Constant(Id)), new ParameterExpression[] { Param });

    return this.DataContext.GetTable<T>().Single(Query);
}

I didn’t write this, I copied it from some person’s blog and it’s worked for me so far. I really don’t know what it does, vague idea, but I’m not capable of translating it to EF.

So, I’ve come here to ask for the assistance of you fine ladies or gentlemen. If possible, I’d love it if someone who knows EF can convert that statement. If there’s alternative code to accomplish the same thing, I’m open to it.

  • 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-18T05:20:48+00:00Added an answer on May 18, 2026 at 5:20 am

    If i understand that method correctly, it returns a single record for any type T, based on a primary key.

    We also have a generic repository, but the interface looks like this:

    public interface IRepository<T> where T : class
    {
       void Add(T entity);
       void Remove(T entity);
       void Attach(T entity);
       IQueryable<T> Find();
    }
    

    And our generic repository implementation:

    public class GenericRepository<T> : IRepository<T> where T : class
    {
       public IQueryable<T> Find()
       {
          return _ctx.GetEntitySet<T>(); // EF plularization/reflection smarts
       }
    }
    

    So to get the equivalent single record for a “Post”:

    var postId = 1;
    IRepository<Post> repository = new GenericRepository<Post>(); // done by DI in reality
    repository
       .Find()
       .SingleOrDefault(x => x.PostId == postId); // explicit predicate to grab record
    

    That’s a lot different to your original code – as the calling code is specifying what the primary key is (or a way to identity a unique record).

    To be honest, trying to dynamically grab a unique record based on a constant primary key value is pretty crazy – what if it’s a composite key? I can’t see how that code would work.

    Happy to see other answers, but i would keep it simple.

    If you want the code to grab the entity set based on T, i can share that – but it’s pretty simple.

    If you want a method to grab a single record, let the calling code supply the predicate/key:

    public T FindSingle(Expression<Func<T,bool>> predicate)
    {
       return _ctx.GetEntitySet<T>.SingleOrDefault(predicate);
    }
    

    Then if for example “Post” had a composite key of “PostName” and “PostType”:

    var singlePost = repository.FindSingle(post => post.PostName == "Foo" && post.PostType == "Question");
    

    In your example, your repository is dictating your model, making each entity have a single column primary key.

    Your repository should aid your model, not define it.

    EDIT – Code for GetEntitySet<T>, as requested

    public IObjectSet<T> GetEntitySet<T>() where T : class
    {
       var entityName = _plularizer.Pluralize(typeof(T).Name);
       string entitySetName = string.Format("{0}.{1}", EntityContainerName, entityName);
       return CreateObjectSet<T>(entitySetName );
    }
    

    Pretty simple, and quite safe, because _plularizer is of type System.Data.Entity.Design.PluralizationService, which is the same module that EF uses to create default entity set names.

    EntityContainerName is your container name for your entities.

    Your _plularizer instance should be static, and kept thread-safe with a fully-locked singleton.

    HTH.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.