具有 Count 和Where 条件的LinQ

发布于 2024-11-05 00:02:42 字数 545 浏览 0 评论 0原文

嗨嗨,我有一个包含以下数据的表:

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

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

发布评论

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

评论(6

浅唱ヾ落雨殇 2024-11-12 00:02:42

出于性能原因,我建议不要使用 Contains,而是使用内置的 Join 方法。

var keys = source.Where(s => s.SampleKey == 3).Select(s => s.SampleID).Distinct();
var result = source.Join(keys, s => s.SampleID, k => k, (s, k) => s);

I would suggest not using Contains, but the built-in Join method for performance reasons..

var keys = source.Where(s => s.SampleKey == 3).Select(s => s.SampleID).Distinct();
var result = source.Join(keys, s => s.SampleID, k => k, (s, k) => s);
庆幸我还是我 2024-11-12 00:02:42
 var idsToSelect = from x in MyTable where x.SampleKey == 3 select x.SampleID;       
 var results = from x in MyTable where idsToSelect.Contains(x.SampleID) select x;
 var idsToSelect = from x in MyTable where x.SampleKey == 3 select x.SampleID;       
 var results = from x in MyTable where idsToSelect.Contains(x.SampleID) select x;
衣神在巴黎 2024-11-12 00:02:42

不确定我是否完全理解这个问题,但这是我的出价:

var results = from r in MyTable
              where r.SampleID == 3 || r.SampleKey == 3
              select r;
var nResults = results.Count();

不过,老实说,我不知道为什么名为 ID 的列实际上不是 ID。 没关系,我想我现在明白了。您将两列作为唯一键链接(或者我希望如此)。

--

编辑

Nappy 实际上有一个很好的解决方案,我不知道为什么她/他删除了它。用 3 抓住所有行然后重新连接它们效果非常好。

Not sure I fully understand the question, but here's my bid:

var results = from r in MyTable
              where r.SampleID == 3 || r.SampleKey == 3
              select r;
var nResults = results.Count();

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.

葮薆情 2024-11-12 00:02:42

在非类似 SQL 的语法中,您可以使用

var groupsById = MyData.GroupBy(x => x.SampleId);
var groupsThatMatch = groupsById.Where(g => g.Any(x => x.SampleKey == 3));
var allRows = groupsThatMatch.SelectMany(g => g);

即按 ID 分组,找到匹配的组,然后将它们展平回行。抱歉,我不知道类似 SQL 的语法。

In the non-SQL-like syntax you could use

var groupsById = MyData.GroupBy(x => x.SampleId);
var groupsThatMatch = groupsById.Where(g => g.Any(x => x.SampleKey == 3));
var allRows = groupsThatMatch.SelectMany(g => g);

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.

音盲 2024-11-12 00:02:42
    DataTable dt = new System.Data.DataTable();

    dt.Columns.Add("SampleID", typeof(Int32));
    dt.Columns.Add("SampleKey", typeof(Int32));
    dt.Columns.Add("SampleData", typeof(string));

    dt.Rows.Add(1, 1, "abc");
    dt.Rows.Add(1, 2, "def");
    dt.Rows.Add(2, 1, "xxx");
    dt.Rows.Add(2, 3, "yyy");
    dt.Rows.Add(3, 3, "zzz");
    dt.Rows.Add(3, 4, "qqq");

    var result = from DataRow myRow in dt.Rows  
                 where (int)myRow["SampleID"] == 3 || (int)myRow["SampleKey"] == 3
                 select myRow;
    DataTable dt = new System.Data.DataTable();

    dt.Columns.Add("SampleID", typeof(Int32));
    dt.Columns.Add("SampleKey", typeof(Int32));
    dt.Columns.Add("SampleData", typeof(string));

    dt.Rows.Add(1, 1, "abc");
    dt.Rows.Add(1, 2, "def");
    dt.Rows.Add(2, 1, "xxx");
    dt.Rows.Add(2, 3, "yyy");
    dt.Rows.Add(3, 3, "zzz");
    dt.Rows.Add(3, 4, "qqq");

    var result = from DataRow myRow in dt.Rows  
                 where (int)myRow["SampleID"] == 3 || (int)myRow["SampleKey"] == 3
                 select myRow;
若沐 2024-11-12 00:02:42

你可能可以这样做:

var result = data.Where(
    y => data.Where(x => x.SampleKey == 3)
             .Select(x => x.SampleID)
             .Contains(y.SampleID));

You could probably do with:

var result = data.Where(
    y => data.Where(x => x.SampleKey == 3)
             .Select(x => x.SampleID)
             .Contains(y.SampleID));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文