Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var invoiceDb = ctx.Invoices.FirstOrDefault(a => a.InvoicerId == InvoicerId &&
- a.Number == invoiceNumber);
- if (invoiceDb == null)
- {
- invoiceDb = new Invoice();
- invoiceDb.Number = invoiceNumber;
- ctx.Invoices.InsertOnSubmit(invoiceDb);
- }
- public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find, Action<T> create) where T : class, new()
- {
- T val = table.FirstOrDefault(find);
- if (val == null)
- {
- val = new T();
- create(val);
- table.InsertOnSubmit(val);
- }
- return val;
- }
- public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find) where T : class, new()
- {
- return FindOrCreate(table, find, a => { });
- }
- var invoiceDb = ctx.Invoices.FindOrCreate(a => a.InvoicerId == InvoicerId &&
- a.Number == invoiceNumber);
- invoiceDb.Number = invoiceNumber;
- var invoiceDb = ctx.Invoices.FindOrCreate(a => a.InvoicerId == InvoicerId &&
- a.Number == invoiceNumber,
- a => a.Number = invoiceNumber);
- public static T FirstOrCreate<T>(this IEnumerable<T> source) where T : class, new()
- {
- var result = source.FirstOrDefault();
- return result != null ? result : new T();
- }
- public static T FirstOrCreate<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate) where T : class, new()
- {
- var result = source.FirstOrDefault(predicate);
- return result != null ? result : new T();
- }
- Invoice selectedInvoice = (from i in Invoices
- where i.ID == invoiceID
- select i).FirstOrCreate();
- Invoice selectedInvoice = db.Invoices.FirstOrCreate(i => i.ID == invoiceID);
- public static T FirstOrCreate<T>(this IEnumerable<T> source, DataClassesDataContext db) where T : class, new()
- {
- var result = source.FirstOrDefault();
- if (result == null)
- {
- result = new T();
- db.GetTable<T>().InsertOnSubmit(result);
- }
- return result;
- }
- Customer selectedCustomer = (from c in db.Customers
- where c.CustomerId == selectedCustomerId
- select c).FirstOrCreate(db);
- var invoice = ctx.Invoices.SingleOrDefault(a => a.InvoicerId == InvoicerId &&
- a.Number == invoiceNumber) ??
- new Invoice();
- Module dbi
- <System.Runtime.CompilerServices.Extension()> _
- 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
- Dim val As T = table.FirstOrDefault(find)
- If val Is Nothing Then
- val = New T()
- create(val)
- table.InsertOnSubmit(val)
- End If
- Return val
- End Function
- <System.Runtime.CompilerServices.Extension()> _
- 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
- Return FindOrCreate(table, find, Function(a)
- End Function)
- End Function
- End Module
- if(invoiceDb == null) ctx.Invoices.InsertOnSubmit(invoiceDB = new Invoice() {Number = invoiceNumber});
Add Comment
Please, Sign In to add comment