Enumerable 和 DB 表之间的 LINQ 内连接
我试图确定当用户提交表单时从数据库中删除哪些记录。 该页面有两个CheckBoxList,一个代表修改前的记录,一个代表修改后的记录。
我可以轻松地获取需要像这样删除的选定值...
//get the items not selected that were selected before
var oldSelectedItems = from oItem in oldChklSpecialNeeds.Items.Cast<ListItem>()
where !(from nItem in newChklSpecialNeeds.Items.Cast<ListItem>()
where nItem.Selected
select nItem.Value).Contains(oItem.Value)
&& oItem.Selected
select oItem.Value;
现在我正在尝试执行类似的操作,但它不允许这样做...
var itemsToDelete = from specialNeed in db.SpecialNeeds
join oldSelectedItem in oldSelectedItems on specialNeed.SpecialNeedsTypeCd equals oldSelectedItem.Value
where specialNeed.CustomerId == customerId
我可以轻松地使用 foreach 循环和 .DeleteOnSubmit()对于每个项目,但我认为有一种方法可以使用 LINQ 的功能并将内部联接的整个查询结果传递给 .DeleteAllOnSubmit()
//like so
db.SpecialNeeds.DeleteAllOnSubmit(itemsToDelete);
有什么想法吗?
I'm trying to determine which records to delete from a database when a user submits a form.
The page has two CheckBoxList one representing the records before modification and one after.
I can easily get the selected values that need to be deleted like this...
//get the items not selected that were selected before
var oldSelectedItems = from oItem in oldChklSpecialNeeds.Items.Cast<ListItem>()
where !(from nItem in newChklSpecialNeeds.Items.Cast<ListItem>()
where nItem.Selected
select nItem.Value).Contains(oItem.Value)
&& oItem.Selected
select oItem.Value;
now I am trying to do something like this but it isn't allowing it...
var itemsToDelete = from specialNeed in db.SpecialNeeds
join oldSelectedItem in oldSelectedItems on specialNeed.SpecialNeedsTypeCd equals oldSelectedItem.Value
where specialNeed.CustomerId == customerId
I can easily just use a foreach loop and a .DeleteOnSubmit() for each item but I'm thinking there is a way use functionality of LINQ and pass the whole query result of an inner join to .DeleteAllOnSubmit()
//like so
db.SpecialNeeds.DeleteAllOnSubmit(itemsToDelete);
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以通过 Contains() 方法在 LINQ to SQL 中使用本地集合。 您可以尝试使用 Contains() 将 join 子句更改为 where:
Local collections can be used in LINQ to SQL with the Contains() method. You can try changing the join clause into a where with Contains():
您遇到的错误是什么? SpecialNeedsTypeCd 和 oldSelectedItem.Value 之间的类型不匹配吗? 您是否刚刚省略了本文第二个 Linq 语句中的 select 或者这就是问题所在?
What is the error you are getting? Is it a type mismatch between SpecialNeedsTypeCd and oldSelectedItem.Value? Have you just omitted the select in the second Linq statement in this post or is that the problem?