gpt4 book ai didi

Linq:计数大于值的情况

转载 作者:行者123 更新时间:2023-12-03 09:24:22 25 4
gpt4 key购买 nike

我有一个 linq 查询,它接受日期和端口组合的列表。此查询必须从表 CruiseCalendar 中返回数据,在该表中可以找到这些组合,但前提是计数大于 1。我无法计算出 groupby 和 count 语法。 var ShipRendezvous 是我陷入困境的地方。

        var dateAndPort = (from r in context.CruiseCalendar
where r.ShipId == shipId
&& r.CruiseDayDate >= dateRange.First
&& r.CruiseDayDate <= dateRange.Last
select new DateAndPort
{
Date = r.CruiseDayDate,
PortId = r.PortId
});

var shipRendezvous = (from r in context.CruiseCalendar
where (dateAndPort.Any(d => d.Date == r.CruiseDayDate
&& d.PortId == r.PortId))
orderby r.CruiseDayDate // (Added since first posting)
select r).ToList();

问候,盖伊

最佳答案

如果我理解正确的话,您正在过滤与 dateAndPort 的任何结果匹配的每个集合,然后希望将其单独分组以获得计数。在分组结果中,您只需要那些出现多次的结果集。

var shipRendezvous = (from r in context.CruiseCalendar
where (dateAndPort.Any(d => d.Date == r.CruiseDayDate
&& d.PortId == r.PortId))
select r)
.GroupBy(x => x.CruiseDayDate) //Groups by every combination
.Where(x => x.Count() > 1) //Where Key count is greater 1
.ToList();

根据您的评论,您希望再次展平列表。为此,请使用 SelectMany():

var shipRendezvous = (from r in context.CruiseCalendar
where (dateAndPort.Any(d => d.Date == r.CruiseDayDate
&& d.PortId == r.PortId))
select r)
.GroupBy(x => x.CruiseDayDate) //Groups by every combination
.Where(x => x.Count() > 1) //Where Key count is greater 1
.SelectMany(x => x)
.ToList();

关于Linq:计数大于值的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24488128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com