Click here to Skip to main content
15,911,531 members
Home / Discussions / Database
   

Database

 
GeneralQuestion about DBCC SHRINKDATABASE() Pin
Xmen Real 11-Jan-08 15:22
professional Xmen Real 11-Jan-08 15:22 
Generaldatabase connection using access database Pin
mon***z11-Jan-08 0:33
mon***z11-Jan-08 0:33 
GeneralRe: database connection using access database Pin
pmarfleet11-Jan-08 1:49
pmarfleet11-Jan-08 1:49 
GeneralRe: database connection using access database Pin
Pete O'Hanlon11-Jan-08 2:58
mvePete O'Hanlon11-Jan-08 2:58 
GeneralRe: database connection using access database Pin
pmarfleet11-Jan-08 3:03
pmarfleet11-Jan-08 3:03 
GeneralMSSQL: removing semi-duplicate entries Pin
ruanr10-Jan-08 22:35
ruanr10-Jan-08 22:35 
GeneralRe: MSSQL: removing semi-duplicate entries Pin
Michael Potter11-Jan-08 11:32
Michael Potter11-Jan-08 11:32 
GeneralLinq to SQL - Writing a generic "GetObjectFromId" method Pin
Dominic Pettifer10-Jan-08 5:20
Dominic Pettifer10-Jan-08 5:20 
I'm trying to write a generic Ling to SQL GetObjectById() method for getting a data object from the database based on a primary key id value, so that instead of having to write...

MyDataContext dataContext = new MyDataContext();<br />
Product myProduct = dataContext.Products.SingleOrDefault<Product>(p => p.Id == productId);


...I instead write...

Product myProduct = Product.FromId(productId);

...which I think is much cleaner.

Now I could simply write individual 'FromId' methods in each mapped data class, but where's the fun in that. So I tried to write a more generic one size fits all method for doing this and came up with the following...

public abstract class BaseTable<T>
{
    public static T FromId(int id)
    {
        MyDatabaseDataContext dataContext = DatabaseContextHelper.GetDatabaseContext();

        StringBuilder sql = new StringBuilder();
        sql.Append("SELECT ");

        MetaTable metaTable = dataContext.Mapping.MappingSource.GetModel(typeof(TopicDatabaseDataContext))
                                        .GetMetaType(typeof(T)).Table;

        foreach (MetaDataMember dm in metaTable.RowType.DataMembers)
        {
            if (dm.DbType != null)
            {
                sql.Append(dm.MappedName).Append(",");
            }
        }

        sql.Remove(sql.Length - 1, 1);

        sql.Append(" FROM ").Append(metaTable.TableName).Append(" WHERE Id = ").Append(id);

        return dataContext.ExecuteQuery<T>(sql.ToString()).FirstOrDefault<T>();
    }
}


I need to ensure all the mapped data classes extend BaseTable<T> but this does seem to work, however, I was wondering if this was the best way of doing this. Wouold this method create any problems, and is there a more efficient way of doing it.

I know this is probably overkill just so I can go from dataContext.Products.SingleOrDefault<Product>(p => p.Id == productId) to Product.FromId(productId) but it's just for a bit of fun, I enjoy the challenge. Smile | :)

Many thanks!

Dominic Pettifer

Blog: www.dominicpettifer.co.uk

GeneralRe: Linq to SQL - Writing a generic "GetObjectFromId" method Pin
Mark Churchill10-Jan-08 13:15
Mark Churchill10-Jan-08 13:15 
GeneralRe: Linq to SQL - Writing a generic "GetObjectFromId" method Pin
Pete O'Hanlon10-Jan-08 21:43
mvePete O'Hanlon10-Jan-08 21:43 
GeneralRe: Linq to SQL - Writing a generic "GetObjectFromId" method Pin
Mark Churchill12-Jan-08 4:30
Mark Churchill12-Jan-08 4:30 
GeneralConditional Index Usage Pin
gnjunge10-Jan-08 3:40
gnjunge10-Jan-08 3:40 
GeneralRe: Conditional Index Usage Pin
Dave-B10-Jan-08 5:18
Dave-B10-Jan-08 5:18 
GeneralRe: Conditional Index Usage Pin
gnjunge10-Jan-08 5:40
gnjunge10-Jan-08 5:40 
GeneralRe: Conditional Index Usage Pin
GuyThiebaut10-Jan-08 5:48
professionalGuyThiebaut10-Jan-08 5:48 
GeneralRe: Conditional Index Usage Pin
gnjunge10-Jan-08 6:07
gnjunge10-Jan-08 6:07 
AnswerRe: Conditional Index Usage Pin
GuyThiebaut10-Jan-08 7:46
professionalGuyThiebaut10-Jan-08 7:46 
GeneralRe: Conditional Index Usage Pin
gnjunge10-Jan-08 19:14
gnjunge10-Jan-08 19:14 
GeneralRe: Conditional Index Usage Pin
Dave-B10-Jan-08 21:53
Dave-B10-Jan-08 21:53 
GeneralRe: Conditional Index Usage Pin
gnjunge10-Jan-08 22:59
gnjunge10-Jan-08 22:59 
GeneralRe: Conditional Index Usage Pin
gnjunge12-Jan-08 23:34
gnjunge12-Jan-08 23:34 
GeneralRe: Conditional Index Usage Pin
GuyThiebaut10-Jan-08 22:17
professionalGuyThiebaut10-Jan-08 22:17 
GeneralSQL database create Pin
topcatalpha9-Jan-08 23:34
topcatalpha9-Jan-08 23:34 
GeneralRe: SQL database create Pin
topcatalpha10-Jan-08 0:30
topcatalpha10-Jan-08 0:30 
QuestionError:An error has occurred while establishing a connection to the server Pin
My Article9-Jan-08 23:31
My Article9-Jan-08 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.