如何将两列相乘并在CoreData中求和? [英] How to multiply two columns and sum it in CoreData?

查看:80
本文介绍了如何将两列相乘并在CoreData中求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件中有商品,我想乘以文件中每种商品的数量和价格并将其相加.

I have a goods in document and I want to multiply quantity and price of every good in document and sum it.

在mySQL中,如下所示:SELECT SUM(price * quantity) FROM documentGoods

In mySQL something like this: SELECT SUM(price * quantity) FROM documentGoods

这是我的代码,但是不起作用.

Here is my code, but it doesn't work.

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:self.tableName inManagedObjectContext:moc]];
[fetch setResultType:NSDictionaryResultType];

NSExpression *multiplyExpression = [NSExpression expressionForFunction:@"multiply:by:" arguments:@[[NSExpression expressionForKeyPath:@"quantity"], [NSExpression expressionForKeyPath:@"price"]]];
NSExpressionDescription *expressionMultipliedDescription = [[NSExpressionDescription alloc] init];
[expressionMultipliedDescription setName:@"multiplied"];
[expressionMultipliedDescription setExpression:multiplyExpression];
[expressionMultipliedDescription setExpressionResultType:NSDecimalAttributeType];

NSExpression *sumExpression = [NSExpression expressionForFunction:@"sum:" arguments:@[[NSExpression expressionForKeyPath:@"multiplied"]]];
NSExpressionDescription *expressionSummaryDescription = [[NSExpressionDescription alloc] init];
[expressionSummaryDescription setName:@"summary"];
[expressionSummaryDescription setExpression:sumExpression];
[expressionSummaryDescription setExpressionResultType:NSDecimalAttributeType];

[fetch setPropertiesToFetch:@[expressionSummaryDescription]];
NSPredicate *searchFilter = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"removed = NO"]]];
[fetch setPredicate:searchFilter];
NSError *error = nil;
NSArray *objects = [moc executeFetchRequest:fetch error:&error];
if(objects && [objects count] > 0)
    return [[objects objectAtIndex:0] valueForKey:@"summary"];
return [[NSDecimalNumber alloc] initWithFloat:.0f];

有人可以帮助我吗?

推荐答案

我遇到了类似的情况,即我的实体是购物车",并且具有价格和数量字段.为了从购物车中获得总和(数量*价格),我使用了以下代码.最后一个NSLog将记录所需的购物车总价格

I faced a similar scenario where my entity is "Cart" and has fields price and quantity. To get sum(quantity * price) from cart i used the following code. The last NSLog would log the required total cart price

NSFetchRequest *request = [[NSFetchRequest alloc] init];

request.entity = [NSEntityDescription entityForName:@"Cart" inManagedObjectContext:self.managedObjectContext];
request.resultType = NSDictionaryResultType;

NSExpressionDescription *productTotalDescr = [[NSExpressionDescription alloc] init];
[productTotalDescr setName:@"productTotal"];
[productTotalDescr setExpression:[NSExpression expressionForFunction:@"multiply:by:"
                                                    arguments:[NSArray arrayWithObjects:
                                                               [NSExpression expressionForKeyPath:@"price"],
                                                               [NSExpression expressionForKeyPath:@"quantity"],
                                                               nil]]];
[productTotalDescr setExpressionResultType:NSInteger64AttributeType];

request.propertiesToFetch = [NSArray arrayWithObjects:productTotalDescr, nil];

NSError *err = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&err];
NSLog(@"%@",[results valueForKeyPath:@"productTotal"]);

这篇关于如何将两列相乘并在CoreData中求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆