无法为LINQ to Entities创建淘汰方法将LINQ to SQL中的DeleteAllOnSubmit()链接 [英] Can 't create extesion method for LINQ to Entities link DeleteAllOnSubmit() in LINQ to SQL
问题描述
因为没有用于LINQ to Entities的DeleteAllOnSubmit()
方法.因此,我自己创建了它,并按以下代码为DeleteAllObjects
命名.
Since there is no DeleteAllOnSubmit()
method for LINQ to Entities. So I myself create it and named for DeleteAllObjects
as following code.
public static void DeleleAllObjects<TEntity>(this ObjectSet<TEntity> objectSet, TEntity[] objects)
{
foreach(var o in objects)
{
objectSet.DeleteObject(o);
}
}
但是我编写的代码编译失败并显示错误消息:
However the code I write fail on compile and get the error message:
类型"TEntity"必须是引用类型,才能将其用作 通用类型或方法中的参数"TEntity"
The type 'TEntity' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method
我认为始终将foreach与DeleteObject一起使用对于删除集合不是一个好主意.
I think use foreach with DeleteObject all the time is not a good idea for delete collection.
推荐答案
尝试一下.该方法必须是通用的,TEntity
只是该类型的占位符.
Try this. The method needs to be a generic, TEntity
is simply the placeholder for the type.
public static void DeleteAllObjects<TEntity>( this ObjectSet<TEntity> objectSet,
IEnumerable<TEntity> objects)
where TEntity : class
{
foreach(var o in objects)
{
objectSet.DeleteObject(o);
}
}
这篇关于无法为LINQ to Entities创建淘汰方法将LINQ to SQL中的DeleteAllOnSubmit()链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!