如何解决在linq查询中添加最大值和平均值
我在下面有一个SQL查询:
[num_elements,2]
输出:
SELECT MAX(r.Suburb),AVG(s.Longitude) AS Longitude,AVG(s.Latitude) AS Latitude
From CafeAddress r inner join Cafe s on s.CafeId = r.CafeId
WHERE r.Region = 'Sydney'
查询应获取该地区的所有郊区,例如悉尼是该地区,郊区将包括Bankstown等。
如何在下面的linq查询中添加最大值和平均值? (区域ID是用户输入(字符串))
Bankstown 174.759541622222 -36.8552809611111
我只能通过分组方式来做到这一点,但我正努力避免这种情况。
解决方法
我认为这将会越来越接近:
var query =
from cafeAddress in _context.CafeAddress
join cafe in _context.Cafe on cafeAddress.CafeId equals cafe.CafeId
where cafeAddress.StateProvinceRegion == regionId
orderby cafeAddress.Suburb descending
group new
{
cafeAddress.Suburb,cafe.Latitude,cafe.Longitude
} by cafeAddress.StateProvinceRegion into g
select new
{
StateProvinceRegion = g.First().Suburb,Latitude = g.Average(x => x.Latitude),Longitude = g.Average(x => x.Longitude),};
或者这个轻微的变体:
var query =
from cafeAddress in _context.CafeAddress
join cafe in _context.Cafe on cafeAddress.CafeId equals cafe.CafeId
where cafeAddress.StateProvinceRegion == regionId
orderby cafeAddress.Suburb descending
group new
{
cafeAddress.Suburb,cafe.Longitude
} by cafeAddress.StateProvinceRegion into gs
from g in gs.Take(1)
select new
{
StateProvinceRegion = g.Suburb,Latitude = gs.Average(x => x.Latitude),Longitude = gs.Average(x => x.Longitude),};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。