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

ekad
  • 14,436
  • 26
  • 44
  • 46
Novice
  • 183
  • 3
  • 14
  • 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. – Tetsuya Yamamoto Aug 28 '18 at 03:52
  • Thanks for the answer. But I still don't get it. Can you give me sample? – Novice Aug 28 '18 at 03:55

2 Answers2

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;
}
Access Denied
  • 8,723
  • 4
  • 42
  • 72
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

Novice
  • 183
  • 3
  • 14
  • I would avoid dynamic if you can, and just use correct types. `public async Task…` and then to fill in values, `response["Credit"] = credit` or `response["Error"] = e`. – ryzngard Aug 30 '18 at 22:33