How to multiply two columns and sum it in CoreData

2019-06-14 05:38发布

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

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

Can anyone please help me?

1条回答
聊天终结者
2楼-- · 2019-06-14 06:28

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"]);
查看更多
登录 后发表回答