Generically return an item from Entity Framework
问题 I have a situation where a website can ask for data from my database based on a string (don't worry - I'm protecting against SQL injections). For various reasons, I would like to have a single method which returns back the object (from EF) that the user expects (which, ultimately, comes back through a partial page). I'm thinking something like this: public <GenericType?> GetObject(int id, string typeName) { switch(typeName) { case "Type1": return db.Type1s.SingleOrDefault(t => t.TypeID == id)