Enumerable 和 DB 表之间的 LINQ 内连接

发布于 2024-07-07 02:02:52 字数 1126 浏览 9 评论 0原文

我试图确定当用户提交表单时从数据库中删除哪些记录。 该页面有两个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独木成林 2024-07-14 02:02:53

可以通过 Contains() 方法在 LINQ to SQL 中使用本地集合。 您可以尝试使用 Contains() 将 join 子句更改为 where

var itemsToDelete = from specialNeed in db.SpecialNeeds
                    where oldSelectedItems.Contains(specialNeed.SpecialNeedsTypeCd)
                       && specialNeed.CustomerId == customerId
                    select ...;

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():

var itemsToDelete = from specialNeed in db.SpecialNeeds
                    where oldSelectedItems.Contains(specialNeed.SpecialNeedsTypeCd)
                       && specialNeed.CustomerId == customerId
                    select ...;
羅雙樹 2024-07-14 02:02:53

您遇到的错误是什么? 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文