tags:

views:

97

answers:

1

Hi,

I'm trying to write a method in C# which can take as parameter a tabletype, column and a columnvalue and check if the got a row with a with value

the method looks like:

public object GetRecordFromDatabase(Type tabletype, string columnname, string columnvalue)

I'm using LINQ to SQL and need to to this in a generic way so I don't need to write each table I got in the DB.

I have been doing this so far for each table, but with more than 70 of these it becomes cumbersome and boring to do.

Is there a way to generate the following code dynamically, And swap out the hardcoded tablenames with the values from the parameterlist? In this example I have a table in the DB named tbl_nation, which the DataContext pluralizes to tbl_nations, and I'm checking the column for the value

if (DB.tbl_nations.Count(c => c.code.Equals(columnvalue)) == 1)
            {
                return DB.tbl_nations.Single(c => c.code.Equals(columnvalue));
            }