I was wondering, after having read this question... he has this code:
public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find)
where T : new()
{
T val = table.FirstOrDefault(find);
if (val == null)
{
val = new T();
table.InsertOnSubmit(val);
}
return val;
}
Would it be possible to also send in that new item as another Func? I mean, of course it would. But, would that have been created already? Or would it be created first when you actually run the Func? Lets say you have this:
public static T FindOrCreate<T>(this Table<T> table, Func<T, bool> find, Func<T> replacement)
where T : new()
{
T val = table.FirstOrDefault(find);
if (val == null)
{
val = replacement();
table.InsertOnSubmit(val);
}
return val;
}
And then called that by doing this:
var invoiceDb = ctx.Invoices.FindOrCreate(a => a.InvoicerId == InvoicerId
&& a.Number == invoiceNumber,
() => new Invoice());
invoiceDb.Number = invoiceNumber;
If that invoice was found, would that new Invoice have been created? Or is that code ran not until the function is actually called? or how does that work?