2

Is there a way to dynmaically define the table name so I do not have to do multiple calls if the table I'm accessing is different for different conditions? I don't want to have an if/else for every table I am accessing if I can use a variable name instead.

using (Entity ctx = new Entity())
{
    dbTableVal = "EntityTables";
    var query = from d in dbTableVal
                where d == "Yes"
                select d;
}

3 Answers 3

1

I guess no, because the table itself also defines the type of returned IQueryable<T>. It is even not possible with ESQL (which has string syntax) to define generic query where you don't know the type of result set.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I tried making this work as a variable, but found that I'm only doing three tables, so I used a switch instead of trying to use a variable.
0

have a look at the below:

Using LINQ to SQL with Dynamic tables

Comments

0

you could create a function that accepts an "object" and "type" and returns an IQUeryable ?

eg:

private IQueryable GetQuery(object Table, Type t)
{
    var query = from d in ((t)Table) where d == "Yes" select d;
    return query;
}

UNTESTED!!! so may need to do some variation on that, but something along those lines might work for you....

1 Comment

I think your method signature would need to be private IQueryable GetQuery<t>(object Table) if you want to use the type in order to do a cast like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.