title: ASP.NET Query Generics DBSet<T>
body: <p>I'm trying to play around with a scenario where i could pass in an Entity model and check if it has a UserFK if it does and the current User isn't in the admin role.. check that the UserFK matches the current User's UserId from the db...</p>
  
  <p>I just can't work out the last bit with the generics.. i think im on the right track but not too sure..</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
  public class IsOwnerAttribute&lt;T&gt; : AuthorizeAttribute where T : class
  {
      public IsOwnerAttribute(IUnitOfWork context)
      {
          this.context = context;
      }
  
      public string RouteParameter
      {
          get { return this.routeParameter; }
          set { this.routeParameter = value; }
      }
  
      public override void OnAuthorization(AuthorizationContext filterContext)
      {
          if (filterContext == null)
          {
              throw new ArgumentNullException("filterContext");
          }
  
          if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
          {
              filterContext.Result = new HttpUnauthorizedResult();
          }
          else if (IsOwner(filterContext))
          {
              return;
          }
          else
          {
              ViewDataDictionary viewData = new ViewDataDictionary();
              viewData.Add("Message", "You do not have sufficient privileges for this operation.");
              filterContext.Result = new ViewResult { ViewName = "Error", ViewData = viewData };
          }
  
      }
  
      bool IsOwner(AuthorizationContext filterContext)
      {
          bool result = false;
  
          int id = -1;
          if (filterContext.RouteData.Values.ContainsKey(this.RouteParameter))
          {
              id = Convert.ToInt32(filterContext.RouteData.Values[this.RouteParameter]);
          }
  
          var currentUser = Membership.GetUser();
          if (currentUser != null &amp;&amp; !filterContext.HttpContext.User.IsInRole("Administrator"))
          {
              var userGuid = (Guid)currentUser.ProviderUserKey;
  
              // Stuck here.. trying to work out how with the Set&lt;T&gt; how i could then check if it has an Id property and a UserFK property and if it does then basically look up if the ID matches the ID in the route and the UserFK matches the userGuid then let them access the content...
              result = context.Set&lt;T&gt;().Where(c =&gt; c.Id == id &amp;&amp; c.UserFK == userGuid).SingleOrDefault() != null;
  
          }
  
          return result;
      }
  
      string routeParameter = "id";
      readonly IUnitOfWork context;
      readonly IDbSet&lt;T&gt; dbset;
  }
  </code></pre>
  
  <p>I don't know if im thinking about it the wrong way or if there's a better way of doing it but would love to know what's possible..</p>