Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-05
14
Medium Priority
?
1,791 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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