fork(2) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5.  
  6. class Foo {
  7. public string ValueA { get; set; }
  8. public int ValueB { get; set; }
  9. }
  10.  
  11. static Func<Func<Foo,T>,T> ValueOrDefault<T>(Foo orig, Foo toReplace, Predicate<T> replaceIf) {
  12. return getValue => {
  13. var origValue = getValue(orig);
  14. var replaceWith = getValue(toReplace);
  15. return replaceIf(origValue) ? replaceWith : origValue;
  16. };
  17. }
  18.  
  19. public static void Main()
  20. {
  21.  
  22.  
  23. Foo fromDb = new Foo();
  24. Foo toSave = new Foo();
  25. Predicate<string> isNullEmpty = string.IsNullOrEmpty;
  26. var valueA = ValueOrDefault(toSave, fromDb, isNullEmpty)(f => f.ValueA);
  27. Predicate<int> isZero = i => i == 0;
  28. var valueB = ValueOrDefault(toSave, fromDb, isZero)(f => f.ValueB);
  29.  
  30.  
  31. }
  32. }
Success #stdin #stdout 0.01s 23936KB
stdin
Standard input is empty
stdout
Standard output is empty