Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am looking for ways to do LINQ on a table selected in runtime via string variable.

This is what I have so far using reflection:

C#
private Entities ctx = new Entities();

public List<AtsPlatform> GetAtsPlatformByName(string atsPlatformName)
{

    List<AtsPlatform> atsPlatform = null;
    System.Reflection.PropertyInfo propertyInfo = ctx.GetType().GetProperty(atsPlatformName.ToLower());
    var platform = propertyInfo.GetValue(ctx, null);

    // it fails here highlighting "platform" with error that reads "Error   1   Could not find an implementation of the query pattern for source type 'System.Data.Objects.ObjectQuery'.  'Select' not found.  Consider explicitly specifying the type of the range variable 'ats'."
    atsPlatform = ((from ats in platform select new AtsPlatform { RequestNumber = ats.RequestNumber, NumberOfFail = ats.NumberOfFail, NumberOfFailWithCR = ats.NumberOfFailWithCR, NumberOfTestCase = ats.NumberOfTestCase }).ToList());

    return atsPlatform;
}


In my model class, I have:

C#
public class AtsPlatform
{
public string Name { get; set; }
public string RequestNumber { get; set; }
public Int32? NumberOfFail { get; set; }
public Int32? NumberOfTestCase { get; set; }
public Int32? NumberOfFailWithCR { get; set; }
}


In Database, I have the following tables: "ats1", "ats2", "ats3" .. "atsN" where each of them has the same entity fields as the properties defined in "AtsPlatform"

What I would like to do is simply:

C#
List<AtsPlatform> a1 = GetAtsPlatformByName("ats1");
List<AtsPlatform> a2 = GetAtsPlatformByName("ats2");
List<AtsPlatform> aN = GetAtsPlatformByName("atsN");


I could use "switch" but this makes the code less expandable and requires update whenever new "ats(N+1)" gets created.

My 2 days of research lead me nowhere but back to ground zero. I'm quite stuck.

PLEASE HELP! Thanks!
Posted
Comments
Mike Meinz 8-Oct-13 19:07pm    
You say that the tables in the database all contain the same column definitions. In that case, a best practice would be to have one table and have a column in that table that defines the DataType. Then your LINQ statement could use that DataType value to access the type of data required. The single table design paradigm saves you research time and ensures your program can handle N+1 DataTypes without modification.
NgSteven 8-Oct-13 19:22pm    
Thanks Mike for your prompt reply.

Do you mind to provide sample code for your "single table design paradigm" approach? I learn better this way. Thanks :)
Mike Meinz 8-Oct-13 19:44pm    
Provide me with the table design (CREATE TABLE statement) of one of your existing tables.

I will add one column to that CREATE TABLE statement named DataType so you can see what it looks like. In that column for each row inserted into the single table, you will put an integer that defines what type of data is in that table row. When you do your LINQ statement, you will include DataType = xxx to indicate which type of data you want to retrieve.
NgSteven 9-Oct-13 19:59pm    
Sorry Mike. I won't be able to provide you the table design since I'm working with remote database.

I agree with your suggestion. Redesigning the table makes the best solution. I'm curently working with the database owner in making appropriate changes.

I will get back to you again once I get this to work or require further assistance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900