2

While working through a tutorial on the entity framework for .net core, I noticed that when looking at the available methods for the DbSet in the code below, after inserting using System.Linq; was vastly different than before.

        private static void SimpleSamuraiQuery()
        {
            using (var context = new SamuraiContext())
            {
                var samurais = context.Samurais. // Add(), Remove(), etc..
            }
        }

After including the using statement, I am suddenly able to see all the Linq methods that you'd expect to see, but it made me a bit curious so I decided to inspect the DbSet class by pressing F12: public abstract class DbSet<TEntity> : IQueryable<TEntity>, IEnumerable<TEntity>, IEnumerable, IQueryable, IAsyncEnumerable<TEntity>, IInfrastructure<IServiceProvider>, IListSource where TEntity : class

I notice that it implements the System.Linq.IQueryable interface, but in the DbSet class itself none of the Linq methods are declared. To me this seems a bit strange, but I decide to dig deeper and check out the IQueryable interface:

namespace System.Linq
{
    //
    // Summary:
    //     Provides functionality to evaluate queries against a specific data source wherein
    //     the type of the data is known.
    //
    // Type parameters:
    //   T:
    //     The type of the data in the data source.
    public interface IQueryable<out T> : IEnumerable<T>, IEnumerable, IQueryable
    {
    }
}

Wtf? Pardon my french, but what is going on here? Why does writing using System.Linq seem to "inject" more methods into a class, and why is the interface seemlingly empty?

6

1 Answer 1

5

What is going on here? Why does writing using System.Linq seem to "inject" more methods into a class, and why is the interface is seemingly empty?

Those methods are extension methods, and they are brought into play by the using directive.

You can do this yourself. Make a namespace that contains a static class with accessible static methods whose first formal is marked this:

namespace Frobber
{
  public interface IBlob 
  {
    int Blob { get; }
  }
  public static class MyExtensions
  {
    public static int AddOne(this IBlob b)
    {
      return b.Blob + 1;
    }
  }
}
... elsewhere ...
using Frobber;
...
IBlob b = whatever();
int x = b.AddOne();

All the "LINQ" methods on IEnumerable<T> and IQueryable<T> are actually extension methods. The reasoning behind this feature is: the IEnumerable<T> and IQueryable<T> methods are the same no matter what the class type is, so it would be onerous to make those methods part of the contract of IEnumerable<T> and IQuerable<T>. Instead, you implement them once, and bring them "into play" with a using directive.

Extension methods are resolved as follows: if "normal" overload resolution fails to find an applicable member, then we start looking for static classes that are top-level members of "used" namespaces; if those static classes have extension methods of the appropriate name, a second round of overload resolution starts with those methods as candidates. Since using directives can happen at different nesting levels, we might end up going to a third round of overload resolution, but in practice this is rare.

why is the interface is seemingly empty?

The interface is not empty. I don't know how you are looking at the contents of the interface but whatever it is, something is going wrong.

IQueryable<T> has members ElementType, Provider and Expression. See https://learn.microsoft.com/en-us/dotnet/api/system.linq.iqueryable?view=netframework-4.8 for the details.

1
  • I am looking at the definition of the interface by pressing F12 on IQueryable in DbSet.cs and I am using Visual Studio Pro 2019. Should I be able to see the contents of this interface? If so, then it is very curious why I'm not able to. Jan 14, 2020 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.