views:

434

answers:

4

I have a procedure:

var Edit = (from R in Linq.Products
            where R.ID == RecordID
            select R).Single();

That I would like to make "Linq.Products" Dynamic. Something like:

    protected void Page_Load(object sender, EventArgs e)
    {
        something(Linq.Products);
    }
    public void something(Object MyObject)
    {
        System.Data.Linq.Table<Product> Dynamic =
            (System.Data.Linq.Table<Product>)MyObject;
        var Edit = (from R in Dynamic
                    where R.ID == RecordID
                    select R).Single();

My problem is that I my "something" method will not be able to know what table has been sent to it. so the static line: System.Data.Linq.Table Dynamic = (System.Data.Linq.Table)MyObject;

Would have to reflect somthing like: System.Data.Linq.Table Dynamic = (System.Data.Linq.Table)MyObject;

With being a dynamic catch all variable so that Linq can just execute the code just like I hand coded it statically. I have been pulling my hair out with this one. Please help.

A: 

Your code doesn't make much sense. Are you trying to receive "something" or send to the "something" method?

Can you post part of the real code, so it makes more sense?

Robert Harvey
I do not have integrated code, right now I am working concept. This will work: protected void Page_Load(object sender, EventArgs e) { something(Linq.Products); } public void something(Object MyObject) { System.Data.Linq.Table<Product> Dynamic = (System.Data.Linq.Table<Product>)MyObject; var Edit = (from R in Dynamic where R.ID == RecordID select R).Single(); }
sorry new to this... I will try through a normal answer of question
A: 

I do not have integrated code because right now I am working concept. This will work:

    protected void Page_Load(object sender, EventArgs e)
    {
        something(Linq.Products);
    }

    public void something(Object MyObject)
    {
        System.Data.Linq.Table<Product> Dynamic = (System.Data.Linq.Table<Product>)MyObject;
        var Edit = (from R in Dynamic
                    where R.ID == RecordID
                    select R).Single();

    }

This will not:

    protected void Page_Load(object sender, EventArgs e)
    {
        something(Linq.AnotherTable);
    }

    public void something(Object MyObject)
    {
        System.Data.Linq.Table<Product> Dynamic = (System.Data.Linq.Table<Product>)MyObject;
        var Edit = (from R in Dynamic
                    where R.ID == RecordID
                    select R).Single();

    }

This will work:

    protected void Page_Load(object sender, EventArgs e)
    {
        something(Linq.AnotherTable);
    }

    public void something(Object MyObject)
    {
        System.Data.Linq.Table<AnotherTable> Dynamic = (System.Data.Linq.Table<AnotherTable>)MyObject;
        var Edit = (from R in Dynamic
                    where R.ID == RecordID
                    select R).Single();

    }

However if you notice, in my "something" method I have to define what table type I am looking for when the problem is my "something" method does not have this capability when applied in my larger code.

woops! I forgot to update my cast. This:System.Data.Linq.Table<AnotherTable> Dynamic = (System.Data.Linq.Table<Product>)MyObject;Should have read:System.Data.Linq.Table<AnotherTable> Dynamic = (System.Data.Linq.Table<AnotherTable>)MyObject;
A: 

OK, I think I can answer your question now. Your problem can be solved by defining a generic type in your method call, and substituting that generic type for your specific type in the method body.

    public void something<T>(T MyObject)
    {
        System.Data.Linq.Table<T> Dynamic = (System.Data.Linq.Table<T>)MyObject;
        var Edit = (from R in Dynamic
                    where R.ID == RecordID
                    select R).Single();

    }

Now you can call it this way:

something<Product>(Linq.Products);

or this way:

something<AnotherTableType>(Linq.AnotherTable);
Robert Harvey
The concept seems sound however when I place the solution in my code I get errors on the: System.Data.Linq.Table<T> Dynamic = (System.Data.Linq.Table<T>)MyObject;andR.IDDo I need to inherit anything special for the solution to work?
A: 

I refer to the answer by Robert Harvey and your comment: "Do I need to inherit anything special for the solution to work?"

Yes, since you are using generics (T), you need to constrain the generic type to something, otherwise T could be anything (object, int, string, Products, etc) and "anything" can have different properties.

So the ideal would be to have a base type or interface, i.e.:

pulbic interface IEntity
{
  Object Id { set; get; }
}

So your table object (entity) needs to inherit from IEntity and then your code would look like this:

protected void Page_Load(object sender, EventArgs e)
{
    something(Linq.AnotherTable);
}

public void something<T>(System.Data.Linq.Table<T> MyObject)
  where T : IEntity
{
    var Edit = (from R in MyObject
                where R.ID == RecordID
                select R).Single();
}

The something method will infer the T for you, as long as AnotherTable table inherits from IEntity.

The next problem is "Id" is now of type object, or whatever type you would like it to be, but it is a fixed type. So you have a problem again, what if you have different ID types? So instead of a object, or whatever you have specified it to be (e.g. int, string, Guid), you might use "Key" as the property with a generic type and the possible solution would be:

pulbic interface IEntity<TKey>
{
  TKey Key { set; get; }
}

This becomes more flexible, but it complicates things even more...

Schalk