【发布时间】:2009-04-24 18:17:38
【问题描述】:
我想知道,在阅读了this question...之后,他有这个代码:
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;
}
是否也可以将该新项目作为另一个 Func 发送?我的意思是,当然会。但是,那会已经创建了吗?还是在您实际运行 Func 时首先创建它?假设你有这个:
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;
}
然后通过这样做调用它:
var invoiceDb = ctx.Invoices.FindOrCreate(a => a.InvoicerId == InvoicerId
&& a.Number == invoiceNumber,
() => new Invoice());
invoiceDb.Number = invoiceNumber;
如果找到该发票,是否会创建新发票?还是该代码在实际调用该函数之前才运行?或者它是如何工作的?
【问题讨论】:
标签: c# generics function delegates