LINQ查询,分组和多个和

时间:2018-07-25 07:07:54

标签: c# entity-framework linq

我有下表:

|   PersonId   |   Year   |   Month   |   Amount   |
----------------------------------------------------
|   123        |   2017   |   3       |   5.5      |
----------------------------------------------------
|   312        |   2017   |   3       |   6        |
----------------------------------------------------
|   888        |   2018   |   3       |   5.5      |
----------------------------------------------------
|   123        |   2018   |   3       |   5.5      |
----------------------------------------------------

我希望得到的是一个带有月份号和2个和的对象。今年/每月的金额和去年相同,但
例如:month:3,sumCurrent:11,sumLast:11.5。

下面的代码获取sumCurrent,但是我不知道如何更改它以获取所需的东西。

var ListOfMonths = MyCollection
                .Where(x => x.Year == Datetime.Now.Year)
                .GroupBy(x => new { x.Month})
                .Select(c => new
                {
                    motnh = c.Key.Month,
                    mysum = Math.Round(((c.Sum(x => x.Amount) / 365) * (decimal)0.99), 0)
                });

3 个答案:

答案 0 :(得分:1)

如果您需要按年月分组,则必须按照以下相同的顺序进行操作,

var listOfMonths = MyCollection.GroupBy(obj => obj.Month).Select(groupingObj => new
            {
                month = groupingObj.Key,
                yearSum = groupingObj.GroupBy(x => x.Year).Select(newGroup => newGroup.Sum(s => s.Amount))
            });

如果您还希望年份也表示哪个年份属于哪个年份,则必须将其包括在select like中,

yearSum = groupingObj.GroupBy(x => x.Year).Select(newGroup => new { year = newGroup.Key, Total = newGroup.Sum(s => s.Amount) })

希望这能回答您的问题。

答案 1 :(得分:1)

不要按月分组,而是按[年,月]组合。

要提高效率,请仅在您要调查的年份执行此操作。

int thisYear = DateTime.UtcNow.Year;
int lastYear = thisYear - 1;

var query = MyCollection
    .Where(item => item.Year >= lastYear)
    .GroupBy(item => new {Year = item.Year, Month = item.Month), // Key
        item => item.Amount)                                     // Values

    // only if you want some ordering:
    .OrderBy(group => group.Key.Year)
    .ThenBy(group => group.Key.Month)

    // calculate the sum of all items in the group. Remember the year and the month
    .Select(group => new
    {
        Year = group.Key.Year,           // omit if you won't use this
        Month = group.Key.Month,
        Sum = group.Sum(),
     });

var thisYearsResult = query.Where(fetchedItem => fetchedItems.Year == thisYear)
    .FirstOrDefault();

年份和月份可能会丢失,因此您不能说lastYear将在[0]中,而今年将在[1]中。这就是我决定保留房产的主要原因

如果您想要一个年份序列,其中每个项目都是一个具有求和值的月份序列,请在GroupBy月份之前执行GroupBy Year

var query = MyCollection
    .Where(item => item.Year >= lastYear)
    .GroupBy(item => item.Year)
    // every group  has Year as key, and all items of that year as elements

    .Select(group => new
    {
         Year = group.Key,
         AmountsPerMonth = group.GroupBy(groupItem => groupItem.Month)
             // every subGroup has the same Year and Month
             .Select(subGroup => new
             {
                 Month = subGroup.Key,
                 Sum = subGroup.Select(subGroupItem => subGroupItem.Amount).Sum(),
             }),
    });

再次,您可能会遇到缺少年份和月份的问题。

答案 2 :(得分:0)

这样,您将可以访问当前和去年的总和。

var input = new List<dataSo> {
    new dataSo(123, 2016, 3, 5.5m ),
    new dataSo(312, 2016, 3, 6m ),
    new dataSo(123, 2017, 3, 5.5m ),
    new dataSo(312, 2017, 3, 6m ),
    new dataSo(888, 2018, 3, 5.5m ),
    new dataSo(123, 2018, 3, 5.5m )
};


var math = input
            // only the last 2 year
            .Where(x => x.Year >= DateTime.Now.Year-1)
            .GroupBy(x => new { x.Year, x.Month })
            .Select(c => new
            {
                Year = c.Key.Year,
                Month = c.Key.Month,
                Sum = c.Sum(x => x.Amount)
            });