Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: CSharp,TEntity,DataContext, Created At: 10/24/2016 8:12:50 PMViews:

HTML view:
Copy Source | Copy HTML
  1. public TEntity Load(DataContext context, object pk)
  2.         {
  3.             var table = context.GetTable<TEntity>();
  4.             return table.Where(this.FilterByPk(pk)).SingleOrDefault();
  5.         }
  6.  
  7.  
  8. protected Expression<Func<TEntity, bool>> FilterByPk(object pk)
  9.         {
  10.             var entity = Expression.Parameter(typeof(TEntity), "ent");
  11.             var keyValue = Expression.Property(entity, primaryKey);
  12.             var primaryKeyValue = Expression.Constant(pk, keyValue.Type);
  13.             var body = Expression.Equal(keyValue, primaryKeyValue);
  14.             var expression = Expression.Lambda<Func<TEntity, bool>>(body, entity);
  15.             return expression;
  16.         }
  17. public void Do()
  18. {
  19. var doc = this.Load(dataContext, documentId);
  20.  
  21.             if (doc == null)
  22.             {
  23.                 doc = new TEntity()
  24.                           {
  25.                               CreationTime = DateTime.Now,
  26.                               id = documentId,
  27.                           };
  28.                 dataContext.GetTable<TEntity>().InsertOnSubmit(doc);
  29.             }
  30. }
  31.  
  32.  

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2018