Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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# 字典c上的算术运算#_C#_Linq_Dictionary_Redis_Hashtable - Fatal编程技术网

C# 字典c上的算术运算#

C# 字典c上的算术运算#,c#,linq,dictionary,redis,hashtable,C#,Linq,Dictionary,Redis,Hashtable,我将这个dictionary对象设置为:-dictionary lst translist 此对象的值为| Key={Id}| Value={QTY=4 | PICKEDUP=2}| 现在我想计算记录的计数,每个记录的数量差和pickeup,然后求数量和,求和pickeup和差异之和。 使用LINQ执行这些算术运算有什么有效的方法吗 我得到的计数为:-int total_transactiondone=lstranslist.count() 对于数量的总和,我想使用'QTY'和'PICKEDU

我将这个dictionary对象设置为:-
dictionary lst translist

此对象的值为
| Key={Id}| Value={QTY=4 | PICKEDUP=2}|

现在我想计算记录的计数,每个记录的数量差和pickeup,然后求数量和,求和pickeup和差异之和。 使用LINQ执行这些算术运算有什么有效的方法吗

我得到的计数为:-
int total_transactiondone=lstranslist.count()

对于数量的总和,我想使用'QTY'和'PICKEDUP'键分割dictionary对象的值,但不知道如何使用它。有什么建议吗

考虑使用这样的东西:

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);
var lstTransList=objRedis.GetAllEntriesFromHash(strHashey);
DataTable dtTransList=新的DataTable();
dtTransList.Columns.Add(“TransId”);
dtTransList.Columns.Add(“数量”,类型(int));
dtTransList.Columns.Add(“PickedUp”,typeof(int));
添加(“UnPickedUp”,typeof(int));
foreach(LSTransList中的KeyValuePair条目)
{
DataRow DR=dtTransList.NewRow();
如果(!string.IsNullOrEmpty(entry.Key))
{
DR[0]=entry.Key;
}
如果(!string.IsNullOrEmpty(entry.Value))
{
clsKeyValueParser objKV=新的clsKeyValueParser(entry.Value);
DR[1]=转换为32(objKV.strGetValue(“数量”,“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(1));
int total_ticketsunpickedup=dtTransList.AsEnumerable().Sum(x=>x.Field(3));
int total_pickedupttransaction=dtTransList.AsEnumerable().Count(行=>row.Field(3)==0);
int total_unpickeduptransaction=dtTransList.AsEnumerable().Count(行=>row.Field(3)!=0);

尝试此操作以获得总数量

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

那有用吗?告诉我们你在哪里遇到了障碍,这样我们可以更好地帮助你。。。除了让我们为您编写整个代码之外,就是说为什么使用字符串作为值而不是类?这会容易得多,而且你不需要做字符串运算。我同意@Arion。在将这些字符串放入字典之前,您应该将它们解析为类(最好是自定义的,但即使是
Tuple
也可以),这样您就不必每次需要对它们进行算术运算时都进行解析。@Arion:我从redis收到了这些数据。RedisClient.Imho linq的GetAllEntriesFromHash方法非常好用,直到您必须滚动阅读它为止。你能用foreach解决这个问题吗?尝试之后,你至少可以问一个更具体的问题。嗨,詹姆斯,谢谢你的帮助。在某些情况下,我的值中只有数量为“|数量=2 |”。在这种情况下,此代码将中断。此外,我还想计算pickeup为0或null的记录计数,以及QTY和pickeup之间的差值为0的事务计数。使用字典非常麻烦,最好创建一个类,用数值存储这些信息。你试过这样做吗?进行字符串操作不是一个非常健壮的解决方案。我从redis获取这些数据。您能推荐任何方法将这些数据存储在具有数值的类中吗?目前我正在使用RedisClientOk的GetAllEntriesFromHash方法GetAllEntriesFromHash返回什么,字符串?它返回字典