views:

43

answers:

2

I have an interesting problem to solve that would be helped by successfully casting objects created by LINQ to SQL into a single master object that I could pass around. Here is the scenario at a high level.

I have a number of stored procedures that fetch data and then all return the exact same columns. The params into the procs and the logic vary greatly, so a single proc will not work. Then Linq creates a strongly typed object which is used throughout my application as parameter and return values.

I am using these strongly typed objects as noted above as parameters and return values in a series of filters used to analyze stocks. My client would like to change the order the order of the filters. The issue is that each succeeding filter will only work on what passed the last filter.

Currently I am hard coding my parameters, and if I could create a master object that I could cast any of these Linq objects to, I could then always pass and return the master object.

I have read the materials available on the internet about casting between different types such as static to anonymous types or a list of integers and an array list containing objects representing integers, but I need to actually cast one object into another.

What general direction would I take to solve this problem of converting strongly typed objects generated by linq that are exactly the same into a single master object?

Thank you for any of your thoughts.

A: 

Although there might be a way to do this with casting, I'm going to offer you a quick and dirty solution - and I'm assuming that your resultant objects are collection-based:

Given that all of your child objects all share the same columns, go ahead and pick one of them to act as your master object - then simply iterate through the rows of your other LINQ objects and add them to the collection of your master object. If your resultant object is a strongly typed data table, then all you'd do is Add to the .Rows collection.

Additionally, you might be able to just add the elements retrieved some subsequent LINQ queries directly to your master object depending upon how you write your SELECT causes in LINQ.

Aaronontheweb
Aaron,Thanks for your suggestion. I am going to be using either an interface or generic types.
MikeMalter
+1  A: 

If all your linq objects have the same fields, you could have them implement an interface defined with those common fields. Then the calls to your filter methods can depend on an interface rather than a specific implementation. In other words, the parameters in the filter methods will be of the interface type rather than a linq class type.

e.g.: Where ICommonFields is an interface you define with all the common fields in each l2s class -

public class Filterer
{
     public ICommonFields filterStuff(ICommonFields x)
     {
       //do stuff
     }

}

or -

public class Filterer 
{
    public T filterStuff<T>(T x)
      where T: class, ICommonFields, new()
    {
        //do stuff
    }

}

I'd prefer the generic version, as T becomes the actual type rather than a reference through an interface - linq-to-sql has issues when using a type through an interface with query expressions.

Edit: sorry, it was late when i first wrote this response (likely excuse! :). Fixed my obvious mistake with the example code :)

Frank Tzanabetis
Frank,Thanks for your insight. This pointed me in the right direction ans was very helpful.
MikeMalter
Did you want to accept it as the answer then? :)
Frank Tzanabetis
There you go....
MikeMalter