如何使用 LINQ>SQL 获得唯一值?

发布于 2024-12-04 21:59:17 字数 355 浏览 1 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(2

铃予 2024-12-11 21:59:17

编辑:好的,之前真的不清楚你的意思,但现在我想我明白你的意思了,你想要这样的东西:

var query = from row in table
            where row.SomeId == targetId
            group row by row.SecondId into g
            where g.Count() == 1
            select g.Single();

换句话说:

  • 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:

var query = from row in table
            where row.SomeId == targetId
            group row by row.SecondId into g
            where g.Count() == 1
            select g.Single();

In other words:

  • Filter by SomeId first
  • Group by SecondId
  • Filter so that only groups with a single entry for that SecondId are returned
  • Select the sole entry from that group

There can be multiple such groups, of course - so you would get (1, 21) and (1, 25) in your example.

打小就很酷 2024-12-11 21:59:17

编辑:如果您想查找 SomeId 和 SomeId 的任意组合; SecondId 该组合有多于一行?那么您可以执行以下操作:

var results = source.Where(x => x.SomeId == 1).GroupBy(x => x.SecondId).Where(g => g.Count > 1);

这将为您提供一组结果,并且仅返回具有多行的结果。因此,在您的示例中,您将得到一个返回 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:

var results = source.Where(x => x.SomeId == 1).GroupBy(x => x.SecondId).Where(g => g.Count > 1);

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.

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