如何从随附的Delete SQL中创建删除LINQ命令?

时间:2013-09-19 22:51:20

标签: c# linq entity-framework linq-to-entities

我正在使用MVC3,EF4.1,C#。

我有这个SQL代码,我想转换为LINQ,但我很难掌握正确的LINQ,所以我正在寻求你的专家LINQ大师的帮助。

从触发器中删除的删除SQL是:

DELETE FROM Feature 
WHERE NOT EXISTS  
(
SELECT    
LU1 AS LUID 
FROM dbo.CLASS WHERE SectionId = Feature.SectionId AND LU1 = Feature.StdLibraryItemId
UNION 
SELECT    
LU2 AS LUID 
FROM dbo.CLASS WHERE SectionId = Feature.SectionId AND LU2 = Feature.StdLibraryItemId
)

提前非常感谢。

1 个答案:

答案 0 :(得分:1)

在EF中看起来像这样(假设你的DbContext是db):

var featuresToRemove = db.Feature.Where(f => !db.Class.Any(c => c.SectionID == f.SectionID && (c.LU1 == f.StdLibraryItemID || c.LU2 == f.StdLibraryItemID ))
foreach (var feature in featuresToRemove)
{
    db.Feature.Remove(feature);
}