How to pass Entity Type names into generic helper, as a parameter?

I want to code a generic helper that takes an entity type name and uses it in its LINQ code.

public Boolean Eval( )
{
    var firstOrDefault = db.Class.OfType<Order>().FirstOrDefault;
}

How do I pass in Order as the type name to make this code more generic? I believe it is do with <T>

Answers


public Boolean Eval<T>( )
{
   var firstOrDefault = db.Class.OfType<T>().FirstOrDefault();
}

If you want that T should always be of a certain base type you could extend it to the following:

public Boolean Eval<T>( ) where T : YourBaseType
{
   var firstOrDefault = db.Class.OfType<T>().FirstOrDefault();
}

Without any constraint there will be a possibility to pass any class (even the one that is not connected with yor entities and your DbContext) which can potentially result in an exception. In such case it might be recommended to wrap you code with some try/catch block.


Need Your Help

How should I properly terminate a 'here string' in powershell v1.0?

powershell

I'm using powershell v1.0 and having a problem terminating a 'here string', the console remains on >> unless I do a CTRL-C at which point I lose the variable:

Impersonation - Access is denied

c# asp.net iis-7 impersonation performancecounter

I am having trouble using impersonation to delete a PerformanceCounterCategory from an MVC website. I have a static class and when the application starts it checks whether or not a