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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:32:00+00:00 2026-05-13T22:32:00+00:00

I’m trying to implement method Find that searches the database. I forgot to mention

  • 0

I’m trying to implement method Find that searches the database.

I forgot to mention that I’m using Postgresql, so I can’t use built in LINQ to SQL.

I want it to be like that:

var user = User.Find(a => a.LastName == "Brown");

Like it’s done in List class. But when I go to List’s source code (thanks, Reflector), I see this:

public T Find(Predicate<T> match)
{
    if (match == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
    }
    for (int i = 0; i < this._size; i++)
    {
        if (match(this._items[i]))
        {
            return this._items[i];
        }
    }
    return default(T);
}

How can I implement this thing? I need to get those parameters to make the search.

Solution

Okay, I understood now that I need to do LINQ to SQL to do all this good expressions stuff, otherwise I’d have to spend a lot of time reimplementeing the wheel.

Since I can’t use LINQ to SQL, I implemented this easy method:

public static User Find(User match, string orderBy = "")
    {
        string query = "";
        if (!String.IsNullOrEmpty(match.FirstName)) query += "first_name='" + match.FirstName + "'";
        if (!String.IsNullOrEmpty(match.LastName)) query += "last_name='" + match.LastName+ "'";
        return Find(query + (!String.IsNullOrEmpty(orderBy) ? orderBy : ""));
    }

This is how to use it:

var user = User.Find(new User { FirstName = "Bob", LastName = "Brown" });
  • 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-13T22:32:01+00:00Added an answer on May 13, 2026 at 10:32 pm

    Your method should accept Expression<Func<User>>.

    This will give you expression tree instead of delegate which you can analyze and serialize to SQL or convert to any other API call your database have.

    If you want everything to be generic, you may wish to go on with implementing IQueryable interface. Useful information can be found here: LINQ Tips: Implementing IQueryable Provider

    Although for a simple scenario I would suggest not to complicate everything and stick with using Expression Trees and returning plain IEnumerable<T> or even List<T>.

    For your case first version of code could look like this:

    public IEnumerable<T> Get(Expression<Func<T, bool>> condition)
    {
        if (condition.Body.NodeType == ExpressionType.Equal)
        {
            var equalityExpression = ((BinaryExpression)condition.Body);
    
            var column = ((MemberExpression)equalityExpression.Left).Member.Name;
    
            var value = ((ConstantExpression)equalityExpression.Right).Value;
    
            var table = typeof(T).Name;
    
            var sql = string.Format("select * from {0} where {1} = '{2}'", table, column, value);
    
            return ExecuteSelect(sql);
        }
    
        return Enumerable.Empty<T>();
    }
    

    And it’s complexity grows fast when you want to handle new and new scenarios so make sure you have reliable unit tests for each scenario.

    C# Samples for Visual Studio 2008 contain ExpressionTreeVisualizer that will help you to dig into Expression Trees more easily to understand how to extract information you need from it.

    And of course, if you can stick with using existing implementation of LINQ, I would suggest to do it. There are Linq to SQL for SQL Server databases, Linq to Entities for many different databases, Linq to NHibernate for NHbernate projects.

    Many other LINQ providers can be found here: Link to Everything: A List of LINQ Providers. Amount of work to implement LINQ provider is not trivial so it’s a good idea to reuse tested and supported solution.

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

Sidebar

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.