具有 Count 和Where 条件的LinQ
嗨嗨,我有一个包含以下数据的表:
SampleID | SampleKey | SampleData
1 | 1 | abc
1 | 2 | def
2 | 1 | xxx
2 | 3 | yyy
3 | 3 | zzz
3 | 4 | qqq
我想检索至少一个 SampleKey 为 3 的所有行,这应该给我
2 | 1 | xxx
2 | 3 | yyy
3 | 3 | zzz
3 | 4 | qqq
包含 2 和 3 的 SampleID 应该返回,因为它们被视为一对。
请建议我怎样才能实现这一目标?可以谢谢!
Hihi, I have a table with the following data:
SampleID | SampleKey | SampleData
1 | 1 | abc
1 | 2 | def
2 | 1 | xxx
2 | 3 | yyy
3 | 3 | zzz
3 | 4 | qqq
I would like to retrieve all rows with at least one SampleKey as 3, which should give me
2 | 1 | xxx
2 | 3 | yyy
3 | 3 | zzz
3 | 4 | qqq
both SampleID with 2 and 3 should be returned as they are considered as one pair.
Pls advice how can I achieve this? May thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
出于性能原因,我建议不要使用
Contains
,而是使用内置的Join
方法。I would suggest not using
Contains
, but the built-inJoin
method for performance reasons..不确定我是否完全理解这个问题,但这是我的出价:
不过,老实说,我不知道为什么名为 ID 的列实际上不是 ID。没关系,我想我现在明白了。您将两列作为唯一键链接(或者我希望如此)。--
编辑
Nappy 实际上有一个很好的解决方案,我不知道为什么她/他删除了它。用 3 抓住所有行然后重新连接它们效果非常好。
Not sure I fully understand the question, but here's my bid:
Though, I'll be honest, I don't know why your column named ID isn't actually an ID.Never mind, I think I get it now. You're linking the two columns as a unique key (or so I hope).--
EDIT
Nappy actually had a great solution, and I'm not sure why s/he deleted it. Grabbing all rows with a 3 then rejoining them works perfect.
在非类似 SQL 的语法中,您可以使用
即按 ID 分组,找到匹配的组,然后将它们展平回行。抱歉,我不知道类似 SQL 的语法。
In the non-SQL-like syntax you could use
i.e group by ID, find the groups that match then flatten those back into rows. I don't know the SQL-like syntax, sorry.
你可能可以这样做:
You could probably do with: