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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:50:22+00:00 2026-06-13T20:50:22+00:00

Is there a way I can make the following db query builder generic? private

  • 0

Is there a way I can make the following db query builder generic?

private IQueryable<Foo> ByName(IQueryable<Foo> dbQuery, Query query)
{
    string[] searchTerms = query.Data.Replace(" ","").ToLower().Split(',');

    if (query.Exclude)
    {
        return dbQuery.Where(x => searchTerms.All(
            y => y != x.Name.Replace(" ", "").ToLower()));
    }

    return dbQuery.Where(x => searchTerms.Any(
        y => y == x.Name.Replace(" ", "").ToLower()));
}

I’ve got the same function for lots of different properties of Foo. ByCounty, ByTown, ByStreet etc etc.

I’ve written some functions that return linq before like the following

public Expression<Func<Foo, bool>> FoosAreWithinDistanceFromGeocode(
    double distance, Geocode geocode)
{
    double distanceSquare = distance * distance;
    return foo => ( SqlFunctions.Square((double)(
        foo.Address.Geocode.Easting - geocode.Easting)) +
        SqlFunctions.Square((double)(fooAddress.Geocode.Northing - 
        geocode.Northing)) ) <= distanceSquare;
}

But I can’t seem to find if the Linq-to-SQL stuff can’t use generics or if it’s possible to pass properties as generics and that kind of thing.


EDIT: I have this working generically for a single search term.

Where [query.Data == "Foo1"]

return dbQuery.Where(SearchMatch("Name", query.Data));

public Expression<Func<Foo, bool>> SearchMatch(string propertyName, string searchTerm)
{
    var foo = Expression.Parameter(typeof(Foo), "foo");
    var prop = Expression.Property(foo, propertyName);
    var search = Expression.Constant(searchTerm);
    var equal = Expression.Equal(prop, search);

    return Expression.Lambda<Func<Foo, bool>>(equal, foo);
}

Anyone have any ideas how to make it work for an array of strings?

  • 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-13T20:50:23+00:00Added an answer on June 13, 2026 at 8:50 pm

    You need to define an interface that exposes the properties that you want to access, like so:

    public interface IHaveName
    {
        string Name { get; }
    }
    

    Then, on your classes, you would implement the interface:

    public class Foo : IHaveName
    

    If you’re using the classes generated from a DBML file, these classes are marked with the partial keyword so implementing the interface is as simple as creating a new file, and inserting:

    public partial class Foo : IHaveName
    

    Since the property is already declared as public in the other .cs file generated from the .dbml file, the interface is implemented implicitly.

    Finally, you would rewrite your ByName method to take a generic type parameter with a constraint that it implement your interface IHaveName:

    private IQueryable<T> ByName<T>(IQueryable<T> dbQuery, Query query)
        where T : IHaveName
    {
        // Everything else is the same.
    

    For your other properties (and methods which use them), you could aggregate them together into one interface, or separate them out, depending on your needs.


    Based on your edit, if you want to create an expression dynamically, you don’t have to give up compile-time safety:

    public Expression<Func<Foo, bool>> SearchMatch(
        Expression<Func<Foo, string>> property, string searchTerm)
    {
        var foo = Expression.Parameter(typeof(Foo), "foo");
        // Get the property info from the property expression.
        var prop = Expression.Property(foo, 
            (property.Body as MemberExpression).Member as PropertyInfo);
        var search = Expression.Constant(searchTerm);
        var equal = Expression.Equal(prop, search);
    
        return Expression.Lambda<Func<Foo, bool>>(equal, foo);
    }
    

    Which you then call like so:

    var expression = SearchMatch(f => f.Name, "searchTerm");
    

    This ensures that the properties that you are passing to SearchMatch actually exist on Foo. Note if you wanted to make this generic for other scalar property types, you would do the following:

    public Expression<Func<Foo, bool>> SearchMatch<T>(
        Expression<Func<Foo, T>> property, T searchTerm)
    {
        var foo = Expression.Parameter(typeof(Foo), "foo");
        // Get the property info from the property expression.
        var prop = Expression.Property(foo, 
            (property.Body as MemberExpression).Member as PropertyInfo);
        var search = Expression.Constant(searchTerm);
        var equal = Expression.Equal(prop, search);
    
        return Expression.Lambda<Func<Foo, bool>>(equal, foo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make this method generic so I can return a
Is there any way I can make Firefox call a JavaScript function in my
Since there is no way that you can make the flash object transparent, there
Is there any way (utilizing Reflection I hope) that I can make an instantiated
Is there any way to make find-name-dired to only show filenames that I can
I'm trying to make only one query out of the following, but can't find
Is there any way can declare a bean in just like JSP UseBean in
Is there way that I can read the file from remote server using fopen
Is there any way I can set a formatter on models that will convert
Is there any way we can fetch X509 Public Cetrificates using c# from AD

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.