C# LINQ查询、分组依据和多个和

C# LINQ查询、分组依据和多个和,c#,entity-framework,linq,C#,Entity Framework,Linq,我有下表: | PersonId | Year | Month | Amount | ---------------------------------------------------- | 123 | 2017 | 3 | 5.5 | ---------------------------------------------------- | 312 | 2017 | 3

我有下表:

|   PersonId   |   Year   |   Month   |   Amount   |
----------------------------------------------------
|   123        |   2017   |   3       |   5.5      |
----------------------------------------------------
|   312        |   2017   |   3       |   6        |
----------------------------------------------------
|   888        |   2018   |   3       |   5.5      |
----------------------------------------------------
|   123        |   2018   |   3       |   5.5      |
----------------------------------------------------
我希望得到的是一个月数为2和的对象;本年度金额/月份与去年金额相同。
例如:月份:3,当前值:11,最后值: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)
                });

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

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)
            });
var输入=新列表{
新数据(1232016,350万),
新数据(312,2016,360万),
新数据(1232017,350万),
新数据(312,2017,360万),
新数据(8882018,350万),
新数据(1232018,3550万)
};
变量数学=输入
//仅过去两年
.Where(x=>x.Year>=DateTime.Now.Year-1)
.GroupBy(x=>新{x.年,x.月})
.选择(c=>new
{
年份=c.Key.Year,
月份=c.Key.Month,
Sum=c.Sum(x=>x.Amount)
});

通过这种方式,您可以访问当前和去年的总和

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)
            });
var输入=新列表{
新数据(1232016,350万),
新数据(312,2016,360万),
新数据(1232017,350万),
新数据(312,2017,360万),
新数据(8882018,350万),
新数据(1232018,3550万)
};
变量数学=输入
//仅过去两年
.Where(x=>x.Year>=DateTime.Now.Year-1)
.GroupBy(x=>新{x.年,x.月})
.选择(c=>new
{
年份=c.Key.Year,
月份=c.Key.Month,
Sum=c.Sum(x=>x.Amount)
});

如果您需要年复一年地进行分组,那么您必须按照如下相同的顺序进行分组

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) })

希望这能回答您的问题。

如果您需要年复一年地进行分组,那么您必须按照如下相同的顺序进行分组

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) })

希望这能回答你的问题。

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

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

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();
年份和月份可能会丢失,因此不能说去年在[0]中,今年在[1]中。这就是我决定年复一年保留房产的主要原因

如果您想要一个年序列,其中每个项目都是一个月序列,具有总和值,请在GroupBy month之前执行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(),
             }),
    });

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

不要按月份分组,而是按[Year,Month]组合分组

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

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();
年份和月份可能会丢失,因此不能说去年在[0]中,今年在[1]中。这就是我决定年复一年保留房产的主要原因

如果您想要一个年序列,其中每个项目都是一个月序列,具有总和值,请在GroupBy month之前执行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个和的对象;本年和去年的金额/月总和。例如:month:3,sumCurrent:11,sumLast:11.5。
按年份和月份进行分组,然后选择它,它将为您提供所需的结果。请详细解释:-
我希望得到的是一个带有月号和2个和的对象;本年和去年的金额/月总和。例如:月:3,sumCurrent:11,sumLast:11.5。
Typo:
groupingObj.Key.month
groupingObj.Key
。编辑它。谢谢:)打字:
groupingObj.Key.Month
groupingObj.Key
。编辑它。谢谢:)它实际上每年会创建两个对象。我想他希望每个月有一个对象,下面列出年数。@CoderofCode,对不起,小姐读了你的评论。什么东西?每年一次,每月一次。像
{Year:2017,Month:3,Sum:11.5},{Year:2017,Month:4,Sum:11.5},{Year:2018,Month:3,Sum:11.0}
它实际上会为每年创建两个对象。我想他希望每个月有一个对象,下面列出年数。@CoderofCode,对不起,小姐读了你的评论。什么东西?每年一次,每月一次。比如
{Year:2017,Month:3,Sum:11.5},{Year:2017,Month:4,Sum:11.5},{Year:2018,Month:3,Sum:11.0}