Linq查询来自其他集合内的集合
本文关键字:集合 其他 查询 Linq | 更新日期: 2023-09-27 18:19:13
我有两个对象:Poll
和PollIp
(一对多)。我想选择所有没有具体Ip地址的民意调查。我该怎么做呢?我的代码:
public Poll GetNextPoll(string ipAddress)
{
return Database.Polls
.Where(p => p.IsPublish.Value && p.PollIps.Any(i => i.IpAdress != ipAddress))
.FirstOrDefault();
}
感谢编辑
在DB中,我有以下内容:
调查:
id Name ...
1 Poll1
2 Poll2
PollIp
PollId IpAdress
1 ::1 (it's my IP)
并且,查询必须返回id等于2的Poll
,因为在PollIp
中没有id等于2的PollId
public IEnumerable<Poll> GetPolls(string ipAddress)
{
return Database.Polls.Where(p => p.PollIps.All(i => i.IpAdress != ipAddress))
}