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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:18:30+00:00 2026-05-21T21:18:30+00:00

I have a method in my project that repeats over and over: public PAC

  • 0

I have a method in my project that repeats over and over:

public PAC PAC_GetByCodiPac(string codiPac)

{

var sel = _gam.PAC.Where(pac => pac.CODI_PAC == codiPac);

            if (sel.Count() > 0)
                return sel.First();
            return null;
        }

The table PAC means (patient), so I have these methods for all the tables I have.
How can I make a generic method for this?
Thanks in advance.

  • 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-21T21:18:31+00:00Added an answer on May 21, 2026 at 9:18 pm

    If you want a generic method that lets you specify any table and any predicate for records from that table then you can’t really get any better than the built-in Where<T>(...) and (as others have already pointed out) the FirstOrDefault<T>(...) extension methods.

    Your code would then look like so:

    var result = _gam.PAC.Where(pac => pac.CODI_PAC == codiPac).FirstOrDefault();
    // OR
    var result = _gam.PAC.FirstOrDefault(pac => pac.CODI_PAC == codiPac);
    

    The best you could get then, writing your own generic method, would be this:

    public T FirstOrDefault<T>(IQueryable<T> source,
        Expression<Func<T, bool>> predicate)
    {
        return source.Where(predicate).FirstOrDefault();
        // OR
        // return source.FirstOrDefault(predicate);
    }
    

    And that is really just redundant. Especially when your calling code would be actually longer using the helper method:

    var result = FirstOrDefault(_gam.PAC, pac => pac.CODI_PAC == codiPac);
    // versus
    var result = _gam.PAC.FirstOrDefault(pac => pac.CODI_PAC == codiPac);
    

    And even worse, your code is no longer using a fluent, composable syntax. This just makes readability and maintenance more difficult.

    If you stick with using the IQueryable<T> extension methods then you can do composition like this:

    var result = _gam.PAC
            .Where(pac => pac.CODI_PAC == codiPac)
            .Where(pac => pac.SomeOtherProperty == someOtherValue)
            .FirstOrDefault();
    // OR
    
    var result = (from pac in _gam.PAC
                  where pac.CODI_PAC == codiPac
                  where pac.SomeOtherProperty == someOtherValue
                  select pac).FirstOrDefault();
    

    One very important thing to note here is that the predicate parameter in the IQueryable<T>.Where<T>(...) extension method is of type Expression<Func<T, bool>>. This allows the IQueryable<T> provider to construct the native SQL (or other native provider query) at the very last moment before returning a result.

    Not using Expression<Func<T, bool>> means that your query would be the equivalent of this:

    var result =
        _gam.PAC
            .ToArray()
            .Where(pac => pac.CODI_PAC == codiPac)
            .FirstOrDefault();
    

    And that would mean the query will load every record from the “PAC” table into memory before selecting the first filtered result and throwing out the rest of the results.

    The bottom-line is that by making a generic helper method you are rewriting existing framework code and you open yourself to performance and maintenance issues while also reducing code readability.

    I hope this helps.

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

Sidebar

Related Questions

I have an ASP.NET (.asmx) Web Service project that has a method like: public
I have a project with a lot of panels. I have one method that
In my rails project I have a presenter method that has a helper view
I have created a new method in one of the project's controllers that it
I have an ASP.NET MVC project with a form. In the Action method that
I have a method in an iOS project that calls avformat_open_input() , it runs
i have a project that uses myBatis for persistence. method A below was working
I have vb6 project that uses Refresh2 method of WebBrowser. How I can access
In project i have reference to Microsoft.Office.Word.Server and in code i have method that
I have a C# method that projects the value of a number from an

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.