相关疑难解决方法(0)

使用LINQ查找C#中数字数组的累积和

我有一个包含双精度的csv字符串(例如"0.3,0.4,0.3"),我希望能够输出一个包含这些数字累积和的双数组(例如[0.3,0.7,1.0]).

到目前为止,我有

double[] probabilities = textBox_f.Text.Split(new char[]{','}).Select(s => double.Parse(s)).ToArray();

它将数字作为数组给出,但不是数字的累积和.

有没有办法继续这个表达式来获得我想要的东西,还是我需要使用迭代从我已经拥有的数组创建一个新的数组?

c# linq arrays cumulative-sum

19
推荐指数
4
解决办法
2万
查看次数

Linq运行总计第一个值加到自身

我有以下计算客户帐户状态的运行总计,但是他的第一个值总是添加到自身,我不知道为什么 - 虽然我怀疑我错过了一些明显的东西:

    decimal? runningTotal = 0;
    IEnumerable<StatementModel> statement = sage.Repository<FDSSLTransactionHistory>()
        .Queryable()
        .Where(x => x.CustomerAccountNumber == sageAccount)
        .OrderBy(x=>x.UniqueReferenceNumber)
        .AsEnumerable()
        .Select(x => new StatementModel()
        {
            SLAccountId = x.CustomerAccountNumber,
            TransactionReference = x.TransactionReference,
            SecondReference = x.SecondReference,
            Currency = x.CurrencyCode,
            Value = x.GoodsValueInAccountCurrency,
            TransactionDate = x.TransactionDate,
            TransactionType = x.TransactionType,
            TransactionDescription = x.TransactionTypeName,
            Status = x.Status,
            RunningTotal = (runningTotal += x.GoodsValueInAccountCurrency)
        });
Run Code Online (Sandbox Code Playgroud)

哪个输出:

29/02/2012 00:00:00 154.80  309.60  
30/04/2012 00:00:00 242.40  552.00  
30/04/2012 00:00:00 242.40  794.40  
30/04/2012 00:00:00 117.60  912.00  
Run Code Online (Sandbox Code Playgroud)

309.60第一排的应该是简单154.80

我做错了什么?

编辑: …

c# linq

10
推荐指数
1
解决办法
319
查看次数

如何将此SQL查询转换为LINQ(OVER(PARTITION BY Date))

这是我想要转换成Linq的查询:

SELECT R.Code, 
       R.FlightNumber, 
       S.[Date], 
       S.Station,
       R.Liters, 
       SUM(R.Liters) OVER (PARTITION BY Year([Date]), Month([Date]), Day([Date])) AS Total_Liters
FROM S INNER JOIN
               R ON S.ID = R.SID
WHERE (R.Code = 'AC')
AND FlightNumber = '124'
GROUP BY  Station, Code, FlightNumber, [Date], Liter
ORDER BY R.FlightNumber, [Date]
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

更新:这是我正在尝试的Linq代码; 我无法按日期进行过度分割.

var test = 
(from record in ent.Records join ship in ent.Ship on record.ShipID equals ship.ID                       

orderby ship.Station
where ship.Date > model.StartView && ship.Date < model.EndView && ship.Station == model.Station && record.FlightNumber == model.FlightNumber …
Run Code Online (Sandbox Code Playgroud)

c# sql linq asp.net-mvc entity-framework

6
推荐指数
1
解决办法
6077
查看次数

Linq-运行总计和子总计

我尝试了一些代码来获得"运行总计"和"小计",但这确实产生了成功的结果.

当给出代码时:

var salesPeople =
new SalesPerson[] 
{ 
new SalesPerson{Name="Ricky", RegionCode="R001",SalesAmount=100,
SalesDate=Convert.ToDateTime("01/Jan/2009")},

new SalesPerson{Name="Mark", RegionCode="R001",SalesAmount=200,
SalesDate=Convert.ToDateTime("02/Jan/2009")},

new SalesPerson{Name="Jon", RegionCode="R001",SalesAmount=400,
SalesDate=Convert.ToDateTime("10/Jan/2009")},

new SalesPerson{Name="Ricky", RegionCode="R001",SalesAmount=100, 
SalesDate=Convert.ToDateTime("05/Jan/2009")},

new SalesPerson{Name="Mark", RegionCode="R001",SalesAmount=200,
SalesDate=Convert.ToDateTime("07/Jan/2009")},

new SalesPerson{Name="Jon", RegionCode="R001",SalesAmount=250,
SalesDate=Convert.ToDateTime("11/Jan/2009")},

new SalesPerson{Name="Ricky", RegionCode="R002",SalesAmount=50,
SalesDate=Convert.ToDateTime("01/feb/2009")},

new SalesPerson{Name="Mark", RegionCode="R002",SalesAmount=120,
SalesDate=Convert.ToDateTime("02/feb/2009")},

new SalesPerson{Name="Peter", RegionCode="R002",SalesAmount=30,
SalesDate=Convert.ToDateTime("10/feb/2009")},

new SalesPerson{Name="Ricky", RegionCode="R002",SalesAmount=400, 
SalesDate=Convert.ToDateTime("05/feb/2009")},

new SalesPerson{Name="Mark", RegionCode="R002",SalesAmount=70,
SalesDate=Convert.ToDateTime("07/feb/2009")},

new SalesPerson{Name="Peter", RegionCode="R002",SalesAmount=60,
SalesDate=Convert.ToDateTime("11/feb/2009")}

};
Run Code Online (Sandbox Code Playgroud)

如何找到与以下类似的Total,SubTotal,RunningTotal.

       (1) Running Total Based on Region and Month

                            Sales History
        -------------------------------------------------------------
        Region Code     Name       MonthyRunningTotal      SalesDate          
                                                           (By Month)

        -------------------------------------------------------------

         R001           Ricky        100 …
Run Code Online (Sandbox Code Playgroud)

c# linq

-1
推荐指数
1
解决办法
8000
查看次数

标签 统计

c# ×4

linq ×4

arrays ×1

asp.net-mvc ×1

cumulative-sum ×1

entity-framework ×1

sql ×1