Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. var invoiceDb = ctx.Invoices.FirstOrDefault(a => a.InvoicerId == InvoicerId &&
  2. a.Number == invoiceNumber);
  3. if (invoiceDb == null)
  4. {
  5. invoiceDb = new Invoice();
  6. invoiceDb.Number = invoiceNumber;
  7. ctx.Invoices.InsertOnSubmit(invoiceDb);
  8. }
  9.  
  10. public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find, Action<T> create) where T : class, new()
  11. {
  12. T val = table.FirstOrDefault(find);
  13. if (val == null)
  14. {
  15. val = new T();
  16. create(val);
  17. table.InsertOnSubmit(val);
  18. }
  19. return val;
  20. }
  21.  
  22. public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find) where T : class, new()
  23. {
  24. return FindOrCreate(table, find, a => { });
  25. }
  26.  
  27. var invoiceDb = ctx.Invoices.FindOrCreate(a => a.InvoicerId == InvoicerId &&
  28. a.Number == invoiceNumber);
  29. invoiceDb.Number = invoiceNumber;
  30.  
  31. var invoiceDb = ctx.Invoices.FindOrCreate(a => a.InvoicerId == InvoicerId &&
  32. a.Number == invoiceNumber,
  33. a => a.Number = invoiceNumber);
  34.  
  35. public static T FirstOrCreate<T>(this IEnumerable<T> source) where T : class, new()
  36. {
  37. var result = source.FirstOrDefault();
  38. return result != null ? result : new T();
  39. }
  40.  
  41. public static T FirstOrCreate<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate) where T : class, new()
  42. {
  43. var result = source.FirstOrDefault(predicate);
  44. return result != null ? result : new T();
  45. }
  46.  
  47. Invoice selectedInvoice = (from i in Invoices
  48. where i.ID == invoiceID
  49. select i).FirstOrCreate();
  50.  
  51. Invoice selectedInvoice = db.Invoices.FirstOrCreate(i => i.ID == invoiceID);
  52.  
  53. public static T FirstOrCreate<T>(this IEnumerable<T> source, DataClassesDataContext db) where T : class, new()
  54. {
  55. var result = source.FirstOrDefault();
  56. if (result == null)
  57. {
  58. result = new T();
  59. db.GetTable<T>().InsertOnSubmit(result);
  60. }
  61. return result;
  62. }
  63.  
  64. Customer selectedCustomer = (from c in db.Customers
  65. where c.CustomerId == selectedCustomerId
  66. select c).FirstOrCreate(db);
  67.  
  68. var invoice = ctx.Invoices.SingleOrDefault(a => a.InvoicerId == InvoicerId &&
  69. a.Number == invoiceNumber) ??
  70. new Invoice();
  71.  
  72. Module dbi
  73. <System.Runtime.CompilerServices.Extension()> _
  74. Public Function FindOrCreate(Of T As {Class, New})(ByVal table As Data.Linq.Table(Of T), ByVal find As Func(Of T, Boolean), ByVal create As Action(Of T)) As T
  75. Dim val As T = table.FirstOrDefault(find)
  76. If val Is Nothing Then
  77. val = New T()
  78. create(val)
  79. table.InsertOnSubmit(val)
  80. End If
  81. Return val
  82. End Function
  83. <System.Runtime.CompilerServices.Extension()> _
  84. Public Function FindOrCreate(Of T As {Class, New})(ByVal table As Data.Linq.Table(Of T), ByVal find As Func(Of T, Boolean)) As T
  85. Return FindOrCreate(table, find, Function(a)
  86.  
  87. End Function)
  88. End Function
  89.  
  90. End Module
  91.  
  92. if(invoiceDb == null) ctx.Invoices.InsertOnSubmit(invoiceDB = new Invoice() {Number = invoiceNumber});
Add Comment
Please, Sign In to add comment