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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:13:51+00:00 2026-06-07T19:13:51+00:00

I am trying to write one linq to sql query and dynamically choose which

  • 0

I am trying to write one linq to sql query and dynamically choose which table to select from at runtime based on an input string provided by the user. Right now I have a selection statement that does this but it just repeats the same query three times, only difference being the selected table. Kinda like this:

if (input == "94")
{
    var query = from i in db.Table94
        select new MyClass(i.A, i.B, i.C);
}

if (input == "95")
{
    var query = from i in db.Table95
        select new MyClass(i.A, i.B, i.C);
}

//more conditional queries

query = query.Where(addtionalFilteringDoneHere);
DataGridView.DataSource = query;

It is easy enough to use conditional Where clauses, but I am looking for something similar for table selections. I tired a couple of things but they didn’t work. Something like:

var query = from i in FetchTable(input)
    select new MyClass(i.A, i.B, i.C);

query = query.Where(addtionalFilteringDoneHere);

public returnType FetchTable(string input)
{
    //need help here
    return db.GetTable<conditionalTable>;
}

I don’t know what the return type would be or even if there a way to do this. Is it possible?

  • 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-07T19:13:52+00:00Added an answer on June 7, 2026 at 7:13 pm

    Since you’re trying to create MyClass instances from different tables you’ve got a problem of either creating a dynamic query – which means you throw out EF or LINQ-to-SQL – or you do a basic lookup instead.

    I would suggest the basic lookup and that you do something like this:

    var fetch = new Dictionary<string, Func<Context, IQueryable<MyClass>>>()
    {
        { "94", db => db.Table94.Select(i => new MyClass(i.A, i.B, i.C)) },
        { "95", db => db.Table95.Select(i => new MyClass(i.A, i.B, i.C)) },
    };
    

    This creates a strongly-typed query within a general purpose dictionary that you can now easily pull out your MyClass instances.

    Something like this:

    var query = fetch[input](db).Where(addtionalFilteringDoneHere);
    DataGridView.DataSource = query;
    

    You could use fetch[input].Invoke(db) if you like that syntax better.

    How does this look as a solution?

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

Sidebar

Related Questions

I need to get two fields from a database table (retrieved using linq-to-sql), one
Hi trying to write a simple linq query from a tutorial I read. But
I'm trying to write a Linq query which returns an array of objects, with
Newbie to LINQ, and trying to write the following query... select f.Section_ID, f.Page_ID, f.SortOrder,
I am trying to write a linq to sql query equivalent to the following
I'm trying to write a Linq query which fetches all users whose first or
I am trying to join a table and a list in LINQ. From posts
I am trying to find a linq query so I can write it in
I am trying to write a LINQ query against two objects ( SPListItemCollection and
Greetings, I'm trying to write a Linq query to run on a list of

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.