0

Context: through a dictionary I try to get the T to inject it in the following method.

Here is the example:

public static bool tryout < T > (Dictionary < T, string > dict, Guid g) 
{
  foreach(var item in dict) 
  {
    var t2 = item.Key;

    var t = GetFromDatabase < t2 > (item.Value, g);
  }
  return false;
}

it is obvious that t2 will not work because it is a variable. How can I use the GetFromDatabase method having the type in my dictionary.

Additional info, I'm working with entity framework (EF6).

public static bool GetFromDatabase < T > (string columnName, Guid g) where T: class {
  try {
    using(var ctx = new Context()) {
      // Get the DbSet of the type T from the entities model (i.e. DB)
      DbSet < T > dbSet = ctx.Set < T > ();
      var obj = new object() as T;

      var key = GetPropertyKey(obj, columnName);
      return dbSet.Any(CreatePredicate < T > (key, g));

    }
  } catch (Exception ex) {
    // Invalid type was provided (i.e. table does not exist in database)
    throw new ArgumentException("Invalid Entity", ex);
  }
}

What I currently have :

DataServiceWriterHelper.GetFromDatabase<Park>("TransmitterId", data.Id);
DataServiceWriterHelper.GetFromDatabase<Haulier>("TransmitterId", data.Id);

What I want to do

Dictionary<object, string> dict = new Dictionary<object, string>();

dict.Add(Park, "TransmitterId");
dict.Add(Haulier, "TransmitterId");
           
var rest = DataServiceWriterHelper.tryout(dict, data.Id);
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

0 Answers0