I have an application where I have a list of database queries to execute.
I know its possible to do something like:
var results = _db.Contacts.SqlQuery( sQuery );
and like:
var results = _db.Database.SqlQuery<Contacts>( sQuery );
But in my situation, I’m working only with metadata. How can I do something like this:
string sBaseType = "Contacts";
var results = _db.Database.SqlQuery<sBaseType>( sQuery );
I had some success with <object>…it returned all the rows I expected it to, but its not in a very usable form. Whats the best way to handle this variety of dynamic query?
Edit —
What I’m hoping to find, is some mechanism rather like this:
var results = _db.Database[sBaseType].SqlQuery( sQuery );
So that I can avoid a construction that looks like :
if (sBaseType == "Contacts") {
var results = _db.Contacts.SqlQuery( sQuery );
} else if (sBaseType == "Buildings") {
var results = _db.Buildings.SqlQuery( sQuery );
} else if (sBaseType == "Rooms") {
var results = _db.Rooms.SqlQuery( sQuery );
} else if (sBaseType == "Equipment") {
var results = _db.Equipment.SqlQuery( sQuery );
}
I don’t need anything particularly fancy…I just am hoping to save building up a 100+ element if-elseif block…
This appears to resolve my main issue ::
Given a bunch of metadata about the query to run, it is able to execute the query and return data in a usable format.