Solved

use generic System.Data.Linq.Table as parametre in c# method

Posted on 2009-05-05
14
1,723 Views
Last Modified: 2013-11-11
Hi there experts,

I'm trying to figure out a way to pass a linq table into a method in c#. The tables passed will have different TEntity's so i can't specify the TEntity in the method header (i.e. i want to use "System.Data.Linq.Table table_" rather than "System.Data.Linq.Table<blah> table_")

The problem is this generic table parametre doesn't work. I get an error when trying to call the method: "the best overloaded method.... has some invalid arguments"

code is below:
public List<CurrentCallBacks> GetAllCallbacks(int database_)

        {

            switch (database_)

            {

                case (int)CurrentDatabase.web:

                   //context.callbacks is type

                   //System.Data.Linq.Table<callbacks>

                    return GetAllCallbacksFromTable(context.callbacks);

                    break;

                case (int)CurrentDatabase.drycleaner:

                 //switch case continues in similar vein;

                    break;

            }
 

         

        }

 

 private List<CurrentCallBacks> GetAllCallbacksFromTable(System.Data.Linq.Table table_)

        {

              //code to return list

        }

Open in new window

0
Comment
Question by:Jazzua
  • 8
  • 6
14 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24310437
You need to make your method generic instead. If the type you're passing is generic, you either need to supply a type, or do the following:
private System.Collections.Generic.List<CurrentCallBacks> GetAllCallbacksFromTable<T>(System.Data.Linq.Table<T> myTable)

{

     // Return list of Callbacks here

}

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24310449
Whoops sorry, forgot one minor detail. Use the following instead. Because the generic type parameter for the Table requires it be a reference type, you must also enforce that constraint on the generic method itself.
private System.Collections.Generic.List<CurrentCallBacks> GetAllCallbacksFromTable<T>(System.Data.Linq.Table<T> myTable) where T: class

{

     // Return list of Callbacks here

}

Open in new window

0
 

Author Comment

by:Jazzua
ID: 24310489
Awesome that solved my first problem.
Now another has arisen.
pretty much what i'm trying to is access infoprmation from the passed table put it into a list which can be passed back to my ui class to be bound to a datagridview. I have a couple of different tables i want to have this functionality for. Its been working fine up until this point except that I had 2 methods that where exactly the same except for the table being used. The problem now is that because the table is of type T it doesn'y recognise the fields from the passed table type (i suppose thats obvious). I could do a conversion, but that defeats the whole purpose of this excercise.
I don't know how clear i'm being with this. It also has just occured to me that maybe the structure of my database is crap. I suppose rather than having two identitical tables in my database to hold data for two different projects I could combine the into one and add a column with a flag indicating which project that row was for. mmmm... may have just answered my own question.
0
 
LVL 9

Expert Comment

by:tculler
ID: 24310519
The only way to solve that problem would be to provide some sort of interface common to all of the types you'll be using for the table. Then, replace the "where T: class" part with "where T: class, INameOfInterface". This will allow you to access any instances of type "T" as if they were actually just instances of the interface. Now you just have to make sure to implement the interface wherever needed. Is this a possibility, or am I way off here?
0
 

Author Comment

by:Jazzua
ID: 24310529
maybe I could access the fields of the TEnitity using an index. is this possible. i.e. myTable[0] etc?
0
 
LVL 9

Expert Comment

by:tculler
ID: 24310555
If you know the type of TEntity beforehand, you should just implement the method like that. For example, if you know that all of the entities will be a class called ExampleClass, you would just use the following code snippet. It is possible to call methods of unknown types using Reflection, but I really don't recommend it in this case. The overhead incurred is severe, and I can already tell that this is not appropriate for this type of problem. If you already know the types of items you'll use (or a common base type of all the items), use that type. If there is a common interface or base type, use what I suggested before. Either come up with your own custom interface or base type (assuming the item(s) don't already have an inherited type).

No, I don't believe the tables are indexed, probably because there are too many dimensions to consider here (rows/columns/relationships/etc.)
private System.Collections.Generic.List<CurrentCallBacks> GetAllCallbacksFromTable(System.Data.Linq.Table<ExampleClass> myTable)

{

     // Return list of Callbacks here

}

Open in new window

0
 

Author Comment

by:Jazzua
ID: 24310587
I think i get what you're saying but the whole reason for this in the first place is that I wanted to use the method for tables with different TEntities. Currently there are a few methods setup to take different tentities but the only thing thats different in them is the tentity. i.e. they different tentities have identical column names/fields.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:tculler
ID: 24310642
If the entities' column/field names are identical, there must be some sort of a base class or interface between them. What types of items are they? .NET Framework, or custom, designed by yourself or another?
0
 

Author Comment

by:Jazzua
ID: 24310680
they are the objects automatically created from table names in my database
0
 
LVL 9

Expert Comment

by:tculler
ID: 24310757
Could you give me an example of how one of these types are generated? Or, for instance, an example of one of the types (and their implementation if it's custom).
0
 

Author Comment

by:Jazzua
ID: 24310808
I'm sorry, I don't know the right term to describe what i'm trying to.
In a windows form application in c# using visual studio 2008 you create a sql database and add tables to it. You can then set up a data source where you add these tables that you have created. You can then create a linq to sql object (not sure of the correct name) and drag tables from the database explorer onto the linq to sql objects designer. Then in your code you can do the following (see code snippet). An object exists with the same name as your table with get/set functionality for the table columns/object fields.
CBProDBDataContext context = new CBProDBDataContext();
 

var callbacks =

                from call in context.callbacks

                select call;
 

//the 'call' item can the be accessed like so:

mystatus = call.status
 
 
 

//where status is a column in the table callbacks which i created at

//design time in my database. Can now be accessed like a method. 

//For instance i can declare an instance of it like so:
 

callback cb = new callback();
 

//this cb object will have fields created from my callbacks table

Open in new window

0
 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
ID: 24310848
Because I'm not too familiar with the syntax above, I guess we'll have to resort to using Reflection, instead. What you can do is the following, though it truly is rather ugly.
Object someObject = /*Get an object from the table.*/;

BindingFlags someBindingFlags = BindingFlags./*Select the appropriate type--i.e., property, method, etc.*/;

Object returnValue = someObject.GetType().InvokeMember("NameOfMember", someBindingFlags, null, someObject, new Object[] { /*List of method's parameters, if any*/ });

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24310861
Sorry, I hit submit too quick. The first String is the name of the method/property/etc. to access. BindingFlags is an enumeration, so don't actually store it beforehand as I did above, just select one within the parameters for the InvokeMember invocation. The next "null" just forces this method call to use the default binder object. The next parameter, someObject is the actual object to call the method on, while the last array of objects is the parameters the method call takes. The return value is returned, and thus stored in "returnValue" above.

This is why I prefer to do my SQL the manual way, with "CREATE TABLE" statements and such, heh...
0
 

Author Closing Comment

by:Jazzua
ID: 31578291
cheers :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now