Linq-to-sql是否不包含? [英] Linq-to-sql Not Contains or Not in?

查看:54
本文介绍了Linq-to-sql是否不包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建民意调查小部件.我有2个表,称它们为Polls和PollsCompleted.我需要执行linq查询,以获取PollsCompleted中给定用户不存在的所有投票.

I'm building a poll widget. I've 2 tables, call them Polls and PollsCompleted. I need to do a linq query to get all the Polls that do not exist for a given user in PollsCompleted.

我有以下几组:

对于民意调查 有效== True

For Polls Where Active == True

对于民意测验已完成 其中UserId == ThisUserId 其中PollId = Polls.Id

For PollsCompleted Where UserId == ThisUserId Where PollId = Polls.Id

现在,我需要获取PollsCompleted中不存在的所有民意调查.我需要一个使用单个或多个查询的示例.我试图将其分解为2个查询.

Now I need to get all Polls that do not exist in PollsCompleted. I need an example for this using either a single or multiple queries. I've tried to break it down into 2 queries.

基本上,我有2个类型为T和T1的IQueryable.我想将所有T都放在T1.ParentId中不存在T.ID的地方.

Basically, I've 2 IQueryables of type T and T1. I want to take all T's where T.ID does not exist in T1.ParentId.

推荐答案

T.Where(x => ! T1.Select(y => y.ParentID).Contains(x.ID))

在Linq中,您通常是自下而上地工作.在这里,我们首先获得T1中所有parentID的集合-T1.Select(...)部分.然后,我们创建一个where子句,以选择ID不包含在该集合中的所有T.

In Linq you often work from the bottom up. Here we first get a collection of all the parentIDs in T1 -- the T1.Select(...) part. Then we create a where clause that selects all of the Ts whose IDs are not contained in that set.

请注意,结果是查询.要实现它,请在上面的语句中使用ToList()或类似的方法.

Note that the result is a query. To materialize it, use ToList() or similar on the statement above.

这篇关于Linq-to-sql是否不包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆