954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help With Linq

Hi,

I am trying to define a 'null' LINQ Table as a global variable and then use a switch statement to deturmine what the Table should be.

I am having problems defining the datatype and also assigning values to it.

Hopefully the code below will explain better.

System.Data.Linq.Table table;

switch (x) {

case '1':
    table = DataClassesDataContext.Table_Name;
    break;
case '2':
    table = DataClassesDataContext.Table_Name2;
    break;
}


Thanks for any help given

Skeldave
Newbie Poster
22 posts since Apr 2011
Reputation Points: 10
Solved Threads: 2
 

System.Data.Linq.Table is a generic type, thus you can't create an object of this type. You must provide the 'parameter' so the compiler can complete the generic class and make it a specific class. For example, you can't have a List<>, you provide a type with it (List) and the compiler 'completes' the class.

That given, if your two classes you wish to assign to this variable have a common base type you can create a Linq.Table of that type and assign them as you are doing. For example. if the classes are derived from "MyDatabase", you could use

System.Data.Linq.Table<MyDatabase> table = null;
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

Hi,

Thanks for the help. I don't know if it matters by im using LINQTOSQL so I have loaded my database into a .dbml layout and I am accessing the classes using:

DataClassesDataContext db = new DataClassesDataContext();

I added your code below:

DataClassesDataContext db = new DataClassesDataContext();
System.Data.Linq.Table<db> table = null;


And there is a problem with the 'db' on the Table part.(db is a Field but used like a Type)

Thanks.

Skeldave
Newbie Poster
22 posts since Apr 2011
Reputation Points: 10
Solved Threads: 2
 

db is a variable, not a type.
You can have a Table of DataClassesDataContext, not a Table of db.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: