0

I want to implement a function that accepts a DbSet (non-generic), a string, and object, and returns DbSet. something like the following pseudu:

public static DbSet Any(DbSet set, string propertyName, objectParameter)
{
  var tableName = set.TableName;
  var columnName = set.GetColumnNameForProperty(propertyName); 
  var query = string.Format("SELECT TOP(1) {0} FROM {1} WHERE {0} = {2}",
                columnName, 
                tableName,
                objectParameter);
}

I think that SQL query is enough since I'll be able to execute it directly on the Database (context.Database.ExecuteSql).

What I want to do is get the table name from the given DbSet, then the column name in the database.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

1

It is not possible from non generic DbSet but this problem can be easily solved by using:

public static IEnumerable<T> Any(DbSet<T> set, string property, objectParameter)
  where T : class
{ ... }

Returning DbSet doesn't make sense because once you query data it is not DbSet anymore.

The bigger problem is getting table name from generic DbSet / ObjectSet because this information is not available from those classes. It is almost impossible to get it at all because it requires accessing non public members of items from MetadataWorkspace.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670