I am using LINQ to SQL in my project.
I have two tables Category and Artifacts with a one-to-many relationship between Category and Artifacts. On the basis of CatgID I am trying to delete artifacts. The query that I have written checks whether any artifact exist with a foreign key of id, then I am grouping Artifact ID to g so as to count so that I can make a condition to pass the Artifact ID as parameter to a function which will delete everything in the Artifacts table.
I am not able to select ArtId using the same range variable ‘a’. Why not? How can I do this or otherwise achieve what I want?
public static void DeleteCategory(int id)
{
var result = from a in adb.Artifacts
where a.CatgId == id
group a by a.ArtId into g
select new { count = g.Count(), **artid = a.ArtId** }; //not able to get ArtId using range variable a.
if (result.count > 0)
{
foreach (var r in result)
{
MyArtifact.DeleteByKey(r.artid);
}
}
}
You can just use g.ArtId because you group by ArtId: