Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# linq to sql - selecting tables dynamically

I have the following scenario: there are a database that generates a new logTable every year. It started on 2001 and now has 11 tables. They all have the same structure, thus the same fields, indexes,pk's, etc.

I have some classes called managers that - as the name says - manages every operation on this DB. For each different table i have a manager, except for this logTable which i have only one manager.

I've read a lot and tried different things like using ITable to get tables dynamically or an interface that all my tables implements. Unfortunately, i lose strong-typed properties and with that i can't do any searches or updates or anything, since i can't use logTable.Where(q=> q.ID == paramId).

Considering that those tables have the same structure, a query that searches logs from 2010 can be the exact one that searches logs from 2011 and on.

I'm only asking this because i wouldn't like to rewrite the same code for each table, since they are equal on it's structure.

EDIT

I'm using Linq to SQL as my ORM. And these tables uses all DB operations, not just select.

like image 211
AdrianoRR Avatar asked Aug 24 '11 17:08

AdrianoRR


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


2 Answers

Consider putting all your logs in one table and using partitioning to maintain performance. If that is not feasible you could create a view that unions all the log tables together and use that when selecting log data. That way when you added a new log table you just update the view to include the new table.

EDIT Further to the most recent comment:

Sounds like you need a new DBA if he won't let you create new SPs. Yes I think could define an ILogTable interface and then make your log table classes implement it, but that would not allow you do GetTable<ILogTable>(). You would have to have some kind of DAL class with a method that created a union query, e.g.

public IEnumerable<ILogTable> GetLogs()
{
    var Log2010 = from log in DBContext.2010Logs
                  select (ILogTable)log;
    var Log2011 = from log in DBContext.2011Logs
                  select (ILogTable)log;
    return Log2010.Concat(Log2011);
}

Above code is completely untested and may fail horribly ;-)

Edited to keep @AS-CII happy ;-)

like image 176
Ben Robinson Avatar answered Nov 15 '22 04:11

Ben Robinson


You might want to look into the Codeplex Fluent Linq to SQL project. I've never used it, but I'm familiar with the ideas from using similar mapping techniques in EF4. YOu could create a single object and map it dynamically to different tables using syntax such as:

public class LogMapping : Mapping<Log> {
    public LogMapping(int year) {
        Named("Logs" + year);
        //Column mappings...
    }
}
like image 24
Steve Danner Avatar answered Nov 15 '22 06:11

Steve Danner