VB中的LINQ C#查询-获取错误

本文关键字:获取 取错误 查询 中的 LINQ VB | 更新日期: 2023-09-27 18:25:22

我有一个价格对象列表(价格包含日期、高点、低点),并试图通过LINQ提取月平均值。我在C#中有这个工作,但我们有一些遗留应用程序在VB.Net中需要它,我似乎无法完全理解转换。我甚至试着把它分解成两个查询,但都无济于事。

工作C#

var prices = from allData in priceList
    group allData by new { month = allData.Date.Month, year = allData.Date.Year } into grp
    select new
    {
        Month = grp.Key.month,
        Year = grp.Key.year,
        AvgHigh = grp.Average(c => c.High),
    };

尝试VB翻译

 dim GroupList = From allData In priceList SELECT New With 
            { _
                .month = allData.Date.Month, _
                .year = allData.Date.Year 
            }
        Dim prices = From grp in GroupList _
        SELECT New With { _
                .Month = grp.month, _
                .Year = grp.year, _
                .AvgHigh = grp.Average(function(c) c.High) _
        }

收到错误:

 BC36610: Name 'Average' is either not declared or not in the current scope.

不太确定从哪里开始。我试过一些在线C#->VB.Net翻译器,但它们对我帮助不大。有什么想法吗?

VB中的LINQ C#查询-获取错误

VB在此类查询中的优势之一是易于在Group By语句中声明范围变量。无论如何,这里有一个可能的VB翻译:

Dim prices = From allData In priceList _
             Group allData By Month = allData.[Date].Month, Year = allData.[Date].Year Into Group _
             Select New With { _
                .Month = Month, _
                .Year = Year, _
                .AvgHigh = Group.Average(Function(c) c.High) _
            }

我认为您将Select而不是Group allData By:

Dim prices = From allData In priceList
    Group allData By New With { _
        Key .month = allData.[Date].Month, _
        Key .year = allData.[Date].Year _
    }
    Select New With { _
        Key .Month = grp.Key.month, _
        Key .Year = grp.Key.year, _
        Key .AvgHigh = grp.Average(Function(c) c.High) _
    }