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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:32:43+00:00 2026-06-08T12:32:43+00:00

Background: I have a web service that returns the rows in a table (table

  • 0

Background:

I have a web service that returns the rows in a table (table name supplied as a parameter) with Ids greater than a certain Id (also supplied as a parameter). We are assuming the Ids are sequential.

I am using Linq to SQL for database interaction so I want to return the new rows as:

List<WhateverObject>

Because we only know the table name at runtime, I can’t use Linq in a normal fashion which has made things much more complex.

Question:

The code is below (and it works). How can I simplify it? It seems overly complex.

private object GetUpdateList(string tableName, int Id, DataClassesDataContext db)
{
    PropertyInfo pi = db.GetType().GetProperty(tableName);

    var table = pi.GetValue(db, null);

    // Get type of object within the table.
    Type genericType = table.GetType().GetGenericArguments()[0];

    // The Where method lives on the Enumerable type in System.Linq
    var whereMethods = typeof(System.Linq.Enumerable)
        .GetMethods(BindingFlags.Static | BindingFlags.Public)
        .Where(mi => mi.Name == "Where");

    // There are actually 2 where methods - we want the one with 2 parameters
    MethodInfo whereMethod = null;
    foreach (var methodInfo in whereMethods)
    {
        var paramType = methodInfo.GetParameters()[1].ParameterType;
        if (paramType.GetGenericArguments().Count() == 2)
        {
            // we are looking for  Func<TSource, bool>, the other has 3
            whereMethod = methodInfo;
            break;
        }
    }

    Func<object, bool> IdEquals = BuildEqFuncFor("Id", Id);

    whereMethod = whereMethod.MakeGenericMethod(genericType);
    var result = whereMethod.Invoke(table, new object[] { table, IdEquals });

    MethodInfo toListMethod = typeof(System.Linq.Enumerable).GetMethod("ToList").MakeGenericMethod(genericType);
    return toListMethod.Invoke(result, new object[] { result });
}

// Build lambda expression for use in Linq
private static Func<object, bool> BuildEqFuncFor(string prop, object val)
{
    // We know we are comparing integers here so cast them.
    // There is probably a more general solution.
    return t => (int)t.GetType().InvokeMember(prop, BindingFlags.GetProperty, null, t, null) > (int)val;
}

To come up with this solution I have had to reference the following questions:

  • How do I invoke an extension method using reflection?
  • How to call extension method "ElementAt"of List<T> with reflection?
  • How to get the type of T from a member of a generic class or method?
  • Convert func to predicate using reflection in C#
  • 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-08T12:32:44+00:00Added an answer on June 8, 2026 at 12:32 pm

    Try something like this:

    private IList GetUpdateList(string tableName, int id, DataClassesDataContext db)
    {
        System.Reflection.PropertyInfo pi = db.GetType().GetProperty(tableName);
    
        var table = pi.GetValue(db, null);
    
        // Get type of object within the table.
        Type genericType = table.GetType().GetGenericArguments()[0];
    
        var param = Expression.Parameter(genericType, "x");
        var predicateExpr = Expression.Lambda(
            Expression.GreaterThan(
                Expression.Property(param, "Id"),
                Expression.Constant(id)),
            param);
    
        return this
            .GetType()
            .GetMethod("GetUpdateListGeneric")
            .MakeGenericMethod(genericType)
            .Invoke(this, new[] { table, predicateExpr }) as IList;
    }
    
    private IList<T> GetUpdateListGeneric<T>(
        Table<T> table, 
        Expression<Func<T, bool>> predicate) where T : class
    {
        return table.Where(predicate).ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a background procss that I need to expose/control as a web
I have a web service that creates a thread to process some data in
Background: I have a Visual Studio solution consisting of a website + REST web-service
I have built a C# .net web service that takes a complex type as
Background I have made a Web Service in Visual Studio, and I'm trying to
Here's the background, I have a webservice from a vendor/partner that returns a large
First, let me start with some background: I have a web service which accepts
In a C# ASP .NET application I have a web service that receives a
Background : I have created a web service client VoucherWebService (consumed from an external
Background We have an asp.net 4.0 web application written in C# that calls a

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.