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?
Queryable
class.