How to multiply two columns and sum it in CoreData?

孤街醉人 提交于 2019-12-08 12:32:44

问题


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:


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


来源:https://stackoverflow.com/questions/25367432/how-to-multiply-two-columns-and-sum-it-in-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!