0
public async Task<WalletTrans> getCredits(int id) 
{
    var credit = await _context.walletTrans.ToAsyncEnumerable().Where(r => r.Id == id).Sum(s => s.quantity);
    return credit;
}

I have that code above using C#.net core that supposedly returning a summation of a filed in the table.

But I'm having this error.

"Cannot implicitly convert type 'decimal' to 'ProjectName.Models.ModelName' [ProjectName]"

2
  • 4
    Your method return type is WalletTrans, but you're returning decimal value from Sum() function. Adjust return type as decimal or assign sum value to any decimal property that WalletTrans class has. Commented Aug 28, 2018 at 3:52
  • Thanks for the answer. But I still don't get it. Can you give me sample? Commented Aug 28, 2018 at 3:55

2 Answers 2

5

a sample as you asked:

public async Task<decimal> getCredits(int id)
{    
    var credit = await _context.walletTrans.ToAsyncEnumerable().Where(r => r.Id == id).Sum(s => s.quantity);   
    return credit;
}
Sign up to request clarification or add additional context in comments.

3 Comments

how can I return this as a JSon?
do you mean a string in json format or some jobject from newtone library? Yes you can, just return not credit, but new JObject(credit) or whatever.
Take a look at the following SO thread: stackoverflow.com/questions/6201529/…
0

This will answer my question regarding on how can I return it as a JSON Object

public async Task<object> getCredits(int id){
    dynamic response = new JObject();
    try {
          decimal credit = await _context.walletTrans.ToAsyncEnumerable().Where(r => r.Id == id).Sum(s => s.quantity);
          response.Credit = credit;
          return response;
        } catch (Exception e) {
            response.Error = e;
            return response;
        }
}

Thanks for All your help :D

1 Comment

I would avoid dynamic if you can, and just use correct types. public async Task<JObject>… and then to fill in values, response["Credit"] = credit or response["Error"] = e.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.