iXDev
iXDev

Reputation: 35

How to combine Mapping Tables classes into one class using LINQ

Hi I am learning LINQ using C# and need some suggestions how to structure my classes.

I have 50 existing tables in an sql server I need to create reports from. These are existing and each have a few years worth of data logging I need to query.

I dragged the tables in using the LINQ to SQL designer and get something similar to this:

[Table(Name = "Data_Log_1")]
public class DataLog1
{
    [Column(Name = "rid", IsPrimaryKey = true)] 
    public int R_Id;

    [Column(Name = "name", CanBeNull = false)]
    public string Name;

    [Column(Name = "Chemical_1_Amount", CanBeNull = false)]
    public int Chemical_1_Amount;

    [Column(Name = "Chemical_2_Amount", CanBeNull = false)]
    public int Chemical_2_Amount;

    // This table continues for another 20 chemicals etc...

}

So on my form I can type query the table and output the result all OK. It returns an instance of DataLog1 with all my data.

So I create my second (of 50) table in the same way:

[Table(Name = "Data_Log_2")]
public class DataLog2
{
    [Column(Name = "rid", IsPrimaryKey = true)] 
    public int R_Id;

    [Column(Name = "name", CanBeNull = false)]
    public string Name;

    [Column(Name = "Chemical_1_Amount", CanBeNull = false)]
    public int Chemical_1_Amount;

    [Column(Name = "Chemical_2_Amount", CanBeNull = false)]
    public int Chemical_2_Amount;

    // This table continues for another 20 chemicals etc...

}

The tables are all identical throughout all 50 data log tables/classes, so I would like to keep them all in one List, or at least be able to pass them as the same type. I cant do this however, because one is a DataLog1, and the other is a DataLog2, even though they are all exactly the same.

Can I create a holder class to do something like this? So both tables are read into the same type?

DataLog log1 = from d in dc.DataLog1
                     select d;

DataLog log2 = from d in dc.DataLog2
                     select d;

I would like to be able to add them all to one big list of type DataLog and iterate through them that way if possible. If so, whats the best way of doing this bearing in mind there are 50 tables of around 25 columns each.

If im doing something stupid feel free to let me know lol. Thanks for any advice.

Upvotes: 3

Views: 1244

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156634

Technically, you could create a projection and use Concat to union all your tables into one big projection that you could query against:

IQueryable<MyDTO> baseQuery = dc.DataLog1.Select(l => new MyDTO {...})
    .Concat(dc.DataLog2).Select(l => new MyDTO {...})
    .Concat(dc.DataLog3).Select(l => new MyDTO {...})
    ...;

But you'll probably find it easier, and see better performance, if you can use a View to join all of these tables together ahead of time to produce a single LINQ collection that you can query against in the first place.

Upvotes: 1

viggity
viggity

Reputation: 15237

The best option is to try to merge all of the tables into a single table and add a column that differentiates which originating table they came from.

If you can't do that, you could create a base class

public class DataLog
{
    [Column(Name = "rid", IsPrimaryKey = true)] 
    public int R_Id;

    [Column(Name = "name", CanBeNull = false)]
    public string Name;

    [Column(Name = "Chemical_1_Amount", CanBeNull = false)]
    public int Chemical_1_Amount;

    [Column(Name = "Chemical_2_Amount", CanBeNull = false)]
    public int Chemical_2_Amount;
}

and then derive child classes for each table

[Table(Name = "Data_Log_1")]
public class DataLog1 : DataLog
{ }

[Table(Name = "Data_Log_2")]
public class DataLog2 : DataLog
{ }

You'd still have to query them all individually, but you could add all the records from each table into a List<DataLog>.

Upvotes: 2

Related Questions