Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq返回基于属性计算出的和的列表_C#_Linq - Fatal编程技术网

C# Linq返回基于属性计算出的和的列表

C# Linq返回基于属性计算出的和的列表,c#,linq,C#,Linq,我有一个发票列表,所有发票的入口日期都是datetime,金额都是double。我需要返回一份过去12个月营业额的双打列表,所以我得到一份所有12个月的列表,如19486.23、52742.19、23653.79等等。可能吗 public async Task<IEnumerable<Double>> GetTurnoverYear() { var invoices = await GetInvoices(); var tu

我有一个发票列表,所有发票的入口日期都是datetime,金额都是double。我需要返回一份过去12个月营业额的双打列表,所以我得到一份所有12个月的列表,如19486.23、52742.19、23653.79等等。可能吗

    public async Task<IEnumerable<Double>> GetTurnoverYear()
    {
        var invoices = await GetInvoices();
        var turnover = invoices.???
    }

    public async Task<IEnumerable<Bill>> GetInvoices()
    {
        var client = new HttpBase();
        using (var httpClient = client.GetBaseClient())
        {
            var response = await httpClient.GetAsync("invoices");
            var result = await response.Content.ReadAsStringAsync();
            dynamic data = JsonConvert.DeserializeObject<dynamic>(result);
            var invoices = JsonConvert.DeserializeObject<IEnumerable<Invoice>>(data.invoices.ToString());
            return invoices;
        }
    }
公共异步任务GetTurnoverYear() { var发票=等待获取发票(); var营业额=发票。??? } 公共异步任务GetInvoices() { var client=newhttpbase(); 使用(var httpClient=client.GetBaseClient()) { var response=wait httpClient.GetAsync(“发票”); var result=await response.Content.ReadAsStringAsync(); 动态数据=JsonConvert.DeserializeObject(结果); var invoices=JsonConvert.DeserializeObject(data.invoices.ToString()); 退回发票; } }
为12个月前的日期创建一个变量,并在何处执行

var list = new List<Invoice> {
new Invoice{entryDate=new DateTime(2019,1,1),amount=2},
new Invoice{entryDate=new DateTime(2019,1,1),amount=2},
new Invoice{entryDate=new DateTime(2020,6,1),amount=1},
new Invoice{entryDate=new DateTime(2020,7,1),amount=1}
};

var aYearAgo = DateTime.Today.AddMonths(-12);
var last12Months = list.Where(l => l.entryDate > aYearAgo).Select(l => l.amount);

//or 
var last12Months = list.Where(l => l.entryDate > DateTime.Today.AddMonths(-12)).Select(l => l.amount);
//this will work the same but its less readable i think
var list=新列表{
新发票{entryDate=新日期时间(2019,1,1),金额=2},
新发票{entryDate=新日期时间(2019,1,1),金额=2},
新发票{entryDate=新日期时间(2020,6,1),金额=1},
新发票{entryDate=新日期时间(2020,7,1),金额=1}
};
var aYearAgo=DateTime.Today.AddMonths(-12);
var LAST12MONTS=list.Where(l=>l.entryDate>aYearAgo)。选择(l=>l.amount);
//或
var last12Months=list.Where(l=>l.entryDate>DateTime.Today.AddMonths(-12))。选择(l=>l.amount);
//这将工作相同,但它的可读性较差,我认为

为12个月前的日期创建一个变量,并在何处执行

var list = new List<Invoice> {
new Invoice{entryDate=new DateTime(2019,1,1),amount=2},
new Invoice{entryDate=new DateTime(2019,1,1),amount=2},
new Invoice{entryDate=new DateTime(2020,6,1),amount=1},
new Invoice{entryDate=new DateTime(2020,7,1),amount=1}
};

var aYearAgo = DateTime.Today.AddMonths(-12);
var last12Months = list.Where(l => l.entryDate > aYearAgo).Select(l => l.amount);

//or 
var last12Months = list.Where(l => l.entryDate > DateTime.Today.AddMonths(-12)).Select(l => l.amount);
//this will work the same but its less readable i think
var list=新列表{
新发票{entryDate=新日期时间(2019,1,1),金额=2},
新发票{entryDate=新日期时间(2019,1,1),金额=2},
新发票{entryDate=新日期时间(2020,6,1),金额=1},
新发票{entryDate=新日期时间(2020,7,1),金额=1}
};
var aYearAgo=DateTime.Today.AddMonths(-12);
var LAST12MONTS=list.Where(l=>l.entryDate>aYearAgo)。选择(l=>l.amount);
//或
var last12Months=list.Where(l=>l.entryDate>DateTime.Today.AddMonths(-12))。选择(l=>l.amount);
//这将工作相同,但它的可读性较差,我认为

假设你不想在一个月中的某个地方开始最后12个月,所以我们首先要计算12个月前的第一天:

DateTime now = DateTime.Now;
DateTime firstDayThisMonth = new DateTime(now.Year, now.Month, 1);
var firstDayOfMonthOneYearAgo = firstDayThisMonth.AddMonths(-12);
我们想摆脱所有比一年前一个月的第一天更早的进入日期:

var newerInvoices = dbContext.Invoices
    .Where(invoice => invoice.EntryDate >= firstDayOfMonthOneYearAgo)
制作同一月份的发票组。我们不希望将本月的发票与一年前一个月的发票(2020年6月与2019年6月不在一起)分组。因此GroupBy必须在年份和月份上:

// parameter keySelector: make groups with same Year / Month as EntryDate:
.GroupBy(invoice => new
{
    Year = invoice.EntryDate.Year,
    Month = invoice.EntryDate.Month}

// Parameter resultSelector: take each year/month combination and all invoices with this
// year/month combination to calculate the sum of all Amounts in the group:
(yearMonth, invoicesInThisYearMonth) => new
{
    Year = yearMonth.Year,
    Month = yearMonth.Month,
    Total = invoicesInThisYearMont.Select(invoice => invoice.Amount).Sum(),
});

注:一年前你将有一个完整的月份(2019年7月),但是对于2020年7月,你只会有直到今天(2020—07—20)

>P的数量之和,假设你不想在一个月中的某个地方开始最后12个月,所以我们将首先计算12个月前的第一天:

DateTime now = DateTime.Now;
DateTime firstDayThisMonth = new DateTime(now.Year, now.Month, 1);
var firstDayOfMonthOneYearAgo = firstDayThisMonth.AddMonths(-12);
我们想摆脱所有比一年前一个月的第一天更早的进入日期:

var newerInvoices = dbContext.Invoices
    .Where(invoice => invoice.EntryDate >= firstDayOfMonthOneYearAgo)
制作同一月份的发票组。我们不希望将本月的发票与一年前一个月的发票(2020年6月与2019年6月不在一起)分组。因此GroupBy必须在年份和月份上:

// parameter keySelector: make groups with same Year / Month as EntryDate:
.GroupBy(invoice => new
{
    Year = invoice.EntryDate.Year,
    Month = invoice.EntryDate.Month}

// Parameter resultSelector: take each year/month combination and all invoices with this
// year/month combination to calculate the sum of all Amounts in the group:
(yearMonth, invoicesInThisYearMonth) => new
{
    Year = yearMonth.Year,
    Month = yearMonth.Month,
    Total = invoicesInThisYearMont.Select(invoice => invoice.Amount).Sum(),
});

注意:一年前,您将拥有完整的月份(2019年7月),但对于2020年7月,您将只拥有截至今天(2020-07-20)的金额总和。

请共享您的代码。我添加了一个位,您需要按发票的月份/年份进行分组,然后合计价值。类似这样的内容:bills.GroupBy(x=>new{month=x.date.month,year=x.date.year});谢谢@jdweng,对我来说是一个很好的解决方案。请分享你的代码。我已经添加了一点,你需要按发票的月份/年份进行分组,然后计算总价值。类似这样的内容:bills.GroupBy(x=>new{month=x.date.month,year=x.date.year});谢谢@jdweng,这对我来说是个不错的解决方案