如何使用 LINQ>SQL 获得唯一值?
使用 LINQ to SQL,如何获取包含 1, 21 的行?我正在寻找
SomeId==1
and
SecondId is a unique entry
SomeId SecondId
0 20
1 21
1 22
1 22
编辑:
好的,抱歉。这还不清楚。我想做的一般是找到该行。可能还有另一个条目看起来像这样:
1 25
And that is the only 25. 所以我会返回两行。在不引用特定 ID 的情况下,如何找到这两行?
Using LINQ to SQL, how do I get the row with 1, 21? I'm looking for
SomeId==1
and
SecondId is a unique entry
SomeId SecondId
0 20
1 21
1 22
1 22
EDIT:
Ok, sorry. That wasn't clear. What I'm trying to do is generically find that row. There might be another entry that looks like this:
1 25
And that is the only 25. So I would get back two rows. Without referencing specific Ids, how do I find these two rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:好的,之前真的不清楚你的意思,但现在我想我明白你的意思了,你想要这样的东西:
换句话说:
SomeId
过滤第一个SecondId
分组SecondId
的单个条目的组当然,可以有多个这样的组 - 所以你会得到在您的示例中为 (1, 21) 和 (1, 25)。
EDIT: Okay, it was really unclear what you meant before, but now I think I see what you mean, and you want something like:
In other words:
SomeId
firstSecondId
SecondId
are returnedThere can be multiple such groups, of course - so you would get (1, 21) and (1, 25) in your example.
编辑:如果您想查找 SomeId 和 SomeId 的任意组合; SecondId 该组合有多于一行?那么您可以执行以下操作:
这将为您提供一组结果,并且仅返回具有多行的结果。因此,在您的示例中,您将得到一个返回 1,22 的组...
如果您正在寻找这样的情况,其中只有表中存在具有该组合的单个条目的行(与我的相反) m 返回)您可以将比较运算符从“>”更改为到'==',另一个回答者也表明了这种可能性。
EDIT: If you saying you would like to find any combination of SomeId & SecondId where there are more than one row for that combination? then you could do the following:
This will give you groups of results, and only return those that have more than one row. So in your example, you would get a group that returns 1,22...
If you are looking for the case where you only have rows in which there is a single entry in the table with that combination (the opposite of what I'm returning) you can change the comparison operator from '>' to '==' and another answer-er has also shown this possibility.