1

I have generic query method which looks like below

List<T> Query<T>(QueryModel query) where T : BaseClass 
{
      // get data and Deserialize by T
}

// where T is Table DTO, to Deserialize response by T

Now I want to implement multiple query execution parallelly

for that I need to have multiple type parameters in response and input and want to achieve something like this:

List<T1, T2, T3> Query<T1, T2, T3>(List<QueryModel> queries) 
     where T1: BaseClass,
     where T2: BaseClass,
     where T3: BaseClass 
{
     // for each query in queries
     // get data for each query and 
     // deserialize each by T1/T2/T3 accordingly
     // return all responses in single generic collection
}

here in above example 3 queries can be there, each can be deserialized by provided typed parameters accordingly.

but I believe we can't have more than one typed parameters with collections like List, Not sure how this can be achieved.

PS : I can go for limited set of Tables for now to query, e.g query with 3-4 typed parameters, so can add overload with fix number of typed parameters, It would be great if there is some solution for n number of typed parameters.

Maulik
  • 510
  • 1
  • 7
  • 22
  • 5
    If your intention is that all of the results go into a single list, the obvious would be to return a `List`. But you've not given a great deal of detail about what you're doing, such that the previous is only a *guess*. – Damien_The_Unbeliever Sep 07 '21 at 12:38
  • 3
    If the goal is to query in parallel, why not just call the first query with different types in parallel? – Magnus Sep 07 '21 at 12:59

2 Answers2

2

You could perhaps write it like

(T1 r1, T2 r2, T3 r3) Query<T1, T2, T3>(QueryModel q1,QueryModel q2,QueryModel q3 )

This would not allow an arbitrary number of queries, but you cannot have a variable number of generic arguments anyway, so I don't see any way to do that while keeping the generic types.

JonasH
  • 28,608
  • 2
  • 10
  • 23
1

From C# 7.0 I believe that you can use tuples e.g.

    List<(T1, T2, T3)> Get<T1,T2,T3>(string query) where T1 : BaseClass, new() where T2 : BaseClass, new() where T3 : BaseClass, new()
    {
        var result = new List<(T1, T2, T3)>
        {
            ( new T1(), new T2(), new T3()),
            ( new T1(), new T2(), new T3())

        };

        return result; 
    }
ChrisBD
  • 9,104
  • 3
  • 22
  • 35