SHARE
TWEET

Untitled

a guest Jan 12th, 2017 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public List<SelectListItem> GetSelectedListFromEntity<T>(Expression<Func<T, object>> property) where T : BaseEntity<int>
  2.     {
  3.          var result = _repository.Query<T>().Select(p => new SelectListItem()
  4.          {
  5.            Text = property, //? (in simple case it looks like:  p.Name + p.Category)
  6.            Value = p.Id.ToString(),
  7.          ).ToList();
  8.  
  9.          return result;
  10.     }
  11.    
  12. var result = GetSelectedListFromEntity<Product>(p => p.Name + p.Category);
  13.    
  14. Expression<Func<T, string>> displayText
  15.    
  16. public List<SelectListItem> GetSelectedListFromEntity<T>(Expression<Func<T, string>> displayText) where T : BaseEntity<int> {
  17.      var result = _repository.Query<T>().Select(p => new SelectListItem() {
  18.        Text = displayText.Compile()(p),
  19.        Value = p.Id.ToString(),
  20.      }).ToList();
  21.  
  22.      return result;
  23. }
  24.    
  25. public string Display<T>(Expression<Func<T, object>> e)
  26. {
  27.     var toStr = e.ToString();  
  28.     return toStr.Substring(toStr.IndexOf("=>")+3).TrimStart('(').TrimEnd(')');      
  29. }
  30.    
  31. Expression<Func<Product, object>> p = t=> t.Name+t.Category;
  32. Expression<Func<Product, object>> p1 = t=> t.Name;
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top