LINQ to SQL, how to write a method which checks if a row exists when we have multiple tables
Posted
by
Beles
on Stack Overflow
See other posts from Stack Overflow
or by Beles
Published on 2009-05-09T21:59:25Z
Indexed on
2010/12/26
17:53 UTC
Read the original article
Hit count: 130
linq-to-sql
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));
}
© Stack Overflow or respective owner