我将此词典对象设为: - Dictionary<string,string> lstTransList;
此对象的值为|Key={Id}|Value={|QTY=4|PICKEDUP=2}|
现在我想计算记录的数量,QTY和&amp;的差异。每个记录的PICKEDUP,然后是QTY的总和,总和PICKEDUP&amp;差异总和。 有没有使用LINQ执行这些算术运算的有效方法?
我算作: - int total_transactiondone = lstTransList.Count();
对于QTY的求和我想用键分割字典对象的值为&#39; QTY&#39; &安培; &#39;拾取&#39;但不知道如何使用它。有什么建议??
考虑使用这样的东西:
decimal total_tickets = lstTransList.Sum(x => Convert.ToDecimal(x.Value));
修改:
现在我使用followin方法来完成这项任务。有没有其他有效的方法来做到这一点?请建议。
var lstTransList = objRedis.GetAllEntriesFromHash(strHashey);
DataTable dtTransList = new DataTable();
dtTransList.Columns.Add("TransId");
dtTransList.Columns.Add("Qty", typeof(int));
dtTransList.Columns.Add("PickedUp", typeof(int));
dtTransList.Columns.Add("UnPickedUp", typeof(int));
foreach (KeyValuePair<string, string> entry in lstTransList)
{
DataRow DR = dtTransList.NewRow();
if (!string.IsNullOrEmpty(entry.Key))
{
DR[0] = entry.Key;
}
if (!string.IsNullOrEmpty(entry.Value))
{
clsKeyValueParser objKV = new clsKeyValueParser(entry.Value);
DR[1] = Convert.ToInt32(objKV.strGetValue("QTY", "0"));
DR[2] = Convert.ToInt32(objKV.strGetValue("PICKEDUP", "0"));
DR[3] = Convert.ToInt32(DR[1]) - Convert.ToInt32(DR[2]);
}
dtTransList.Rows.Add(DR);
}
int total_transactiondone = dtTransList.Rows.Count;
int total_tickets = dtTransList.AsEnumerable().Sum(x => x.Field<int>(1));
int total_ticketsunpickedup = dtTransList.AsEnumerable().Sum(x => x.Field<int>(3));
int total_pickeduptransaction = dtTransList.AsEnumerable().Count(row => row.Field<int>(3) == 0);
int total_unpickeduptransaction = dtTransList.AsEnumerable().Count(row => row.Field<int>(3) != 0);
答案 0 :(得分:0)
尝试此操作以获得总数QTY
decimal total_tickets_qty = lstTransList.Sum(x => Convert.ToDecimal(Regex.Match(x.Value.Split('|')[1], @"\d+").Value);
PICKEDUP总计
decimal total_tickets_pickedup = lstTransList.Sum(x => Convert.ToDecimal(Regex.Match(x.Value.Split('|')[2], @"\d+").Value));
差异总和
decimal total_tickets = lstTransList.Sum(x => Convert.ToDecimal(Regex.Match(x.Value.Split('|')[1], @"\d+").Value) - Convert.ToDecimal(Regex.Match(x.Value.Split('|')[2], @"\d+").Value));