-2

Here is my method. I want to return the value of the variables and assign it to the model.

How to return values from the variables item1 and item2?

public async Task <(int, int)> GetCount(int CustId, string userId)
{
    _log.Info($"Enter- GetCount");
    const string query = @" Select count(*) from Products
                            Select count(*) from Units";
    using (IDbConnection myConnection = 
    _getConnection.GetConnection(GetConnectionStringFromDict(userId)))
    {
        _log.Info($"Exit- GetCount");
        using (var multi = await myConnection.QueryMultipleAsync(query, new { CustId 
        }).ConfigureAwait(false))
        {
            var item1 = multi.ReadFirstOrDefaultAsync<int>();
            var item2 = multi.ReadFirstOrDefaultAsync<int>();
        }
    }
    using (IDbConnection myConnection = 
    _getConnection.GetConnection(GetConnectionStringFromDict(userId)))
    {
        _log.Info($"Exit- GetCount");
        return (await myConnection.QueryAsync<(int, int)>(query, new { CustId 
        }).ConfigureAwait(false)).FirstOrDefault();
    }
}
Fildor
  • 14,510
  • 4
  • 35
  • 67
Suvarna S
  • 1
  • 1
  • 1
  • If you want to return `item1` and `item2` as a tuple, you can use `return (item1, item2)`. It's unclear what would happen to your existing `return` in this case though! – canton7 May 07 '21 at 10:56
  • Unrelated: You don't seem to use `CustId` in your queries? Is that right? – Fildor May 07 '21 at 10:57
  • From a readability standpoint: Yes you can return a Tuple. But I'd actually suggest returning a ResultClass (or Struct or since recently: Record), so you can have named Properties "ProductsCount" and "UnitsCount" ... but that's quite opinionated. – Fildor May 07 '21 at 11:02
  • @Fildor : I have missed adding based on custID, But Im not getting how to return values from item1 and item2 – Suvarna S May 07 '21 at 11:03
  • @canton7: return (item1, item2) I have tried this, but no luck. – Suvarna S May 07 '21 at 11:04
  • Have a look at @JohnathanBarclays's answer. I think he's on to something. – Fildor May 07 '21 at 11:04
  • 1
    @SuvarnaS Post what you tried, and what makes you think you had "no luck". Unfortunately the batteries in my mind-reading device are flat today, so you'll have to do the hard work and actually tell me what's wrong :) – canton7 May 07 '21 at 11:05
  • ^^ What part are struggling with? To return a Tuple or that the returned tuple doesn't have the desired values? Or both? – Fildor May 07 '21 at 11:06
  • Does this answer your question? [Return multiple values to a method caller](https://stackoverflow.com/questions/748062/return-multiple-values-to-a-method-caller) – Michael Freidgeim Nov 05 '21 at 01:29

3 Answers3

3

Using ReadFirstOrDefaultAsync will return the product count twice.

You need to read the results as a collection, which can be converted into a tuple:

public async Task <(int, int)> GetCount(int CustId, string userId)
{
    _log.Info($"Enter- GetCount");
    const string query = @" Select count(*) from Products
                            Select count(*) from Units";
    using (IDbConnection myConnection = _getConnection
        .GetConnection(GetConnectionStringFromDict(userId)))
    {
        _log.Info($"Exit- GetCount");
        using (var multi = await myConnection.QueryMultipleAsync(query, new { CustId })
            .ConfigureAwait(false))
        {
            var items = await multi.ReadAsync<int>();
            return (items.First(), items.Last());
        }
    }
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • I am getting the values of both variables here. But not getting how to return the values. var item = multi.ReadFirstOrDefaultAsync(); var item1 = multi.ReadFirstOrDefaultAsync(); – Suvarna S May 07 '21 at 11:41
  • @SuvarnaS _"But not getting how to return the values."_ It's literally part of the answer: `return (items.First(), items.Last());` What exactly is it you don't get about it? – Fildor May 07 '21 at 13:05
2

You can use a Tuple to return multiple items from a function call.

public async Task<Tuple<int, int>> GetCount(int CustId, string userId) {
  ...
  return new Tuple<int, int>(item1, item2);
}

Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples

Jason D
  • 1,863
  • 2
  • 16
  • 30
2

You can return ValueTuples by just putting them in parentheses.

static (int, int) test()
{
    int a = 0, b = 1;
    return (a, b);
}
Jack T. Spades
  • 986
  • 6
  • 9
  • 3
    @SuvarnaS "No Luck" is _not_ enough a description for failure. What makes you think you had "no luck"? A compiler error? Runtime Exception? Wrong results? – Fildor May 07 '21 at 11:07