I have the following code:
var tagToPosts = (from t2p in dataContext.TagToPosts
join t in dataContext.Tags on t2p.TagId equals t.Id
select new { t2p.Id, t.Name });
//IQueryable tag2postsToDelete;
foreach (Tag tag in tags)
{
Debug.WriteLine(tag);
tagToPosts = tagToPosts.Where(t => t.Name != tag.Name);
}
IQueryable tagToPostsToDelete = (from t2p in dataContext.TagToPosts
join del in tagToPosts on t2p.Id equals del.Id
select t2p);
dataContext.TagToPosts.DeleteAllOnSubmit(tagToPostsToDelete);
where tags
is a List<Tag>
- list of tags created by constructor, so they have no id.
I run the the code putting a brakepoint on line with Debug and wait for a few cycles to go. Then I put tagToPosts.ToList() in the Watch window for query to execute.
In SQL profiler I can see the following query:
exec sp_executesql N'SELECT [t0].[Id], [t1].[Name]
FROM [dbo].[tblTagToPost] AS [t0]
INNER JOIN [dbo].[tblTags] AS [t1] ON [t0].[TagId] = [t1].[Id]
WHERE ([t1].[Name] @p0) AND ([t1].[Name] @p1) AND ([t1].[Name] @p2)',N'@p0 nvarchar(4),@p1 nvarchar(4),@p2 nvarchar(4)',@p0=N'tag3',@p1=N'tag3',@p2=N'tag3'
We can see every parameter parameter have the value of last tag.Name
in a cycle.
Have you any idea on how to get arout this and get cycle to add Where
with new condition every tyme? I can see IQueryable stores only pointer to variable before execution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…