1

I understand the preference is given to instance method over extension method during compile time resolution. However for LinqToSQL (or for LinqtoXXX) :

Table<Order> orders = context.Orders
var query = orders.Where(o=>o.name=="xyz")

What is deciding factor for resolving above Where method of Queryable class ? Inspite of Table<Order> implements both IEnumerabe<T> and IQueryable<T>

public sealed class Table<TEntity> : IQueryable<TEntity>, IQueryable, 
                                         IEnumerable<TEntity>, IEnumerable,
                                         ITable<TEntity>, ITable,
                                         IQueryProvider, 
                                         IListSource
        where TEntity : class
    {
        // ...
    }
rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44

1 Answers1

0

EntityFramework uses IQueryable to enable Linq-To-Sql for context.Orders.

The difference is that IQueryable is the interface that allows LINQ-to-SQL (LINQ.-to-anything really) to work. So if you further refine your query on an IQueryable, that query will be executed in the database, if possible.

For the IEnumerable case, it will be LINQ-to-object, meaning that all objects matching the original query will have to be loaded into memory from the database.

For more info see: Returning IEnumerable<T> vs. IQueryable<T>

Edit 29/01/2018

If your class has extension methods with the same name defined for 2 interfaces Here's what the results will be:

public interface IA
{
    void SayHello();
}
public interface IB
{
    void SayHello();
}

public class AB: IA,IB
{
    //If two interfaces have methods with same signature, 
    //They can both be implemented by prefixing interface name for one of them.
    void IA.SayHello()
    {
        Console.WriteLine("Hello A");
    }

    void IB.SayHello()
    {
        Console.WriteLine("Hello B");
    }

    public void SayHello()
    {
        Console.WriteLine("Hello AB");
    }
}

public static class MyClassExt
{
    public static void SayBye(this IA a)
    {
        Console.WriteLine("Bye from A");
    }

    public static void SayBye(this IB b)
    {
        Console.WriteLine("Bye from B");
    }

    public static void SayBye(this AB ab)
    {
        Console.WriteLine("Bye from AB");
    }
}

var obj = new AB();

obj.SayHello(); //Hello AB
((IA)obj).SayHello(); //Hello A
((IB)obj).SayHello(); //Hello B

obj.SayBye(); //Bye AB
((IA)obj).SayBye(); //Bye A
((IB)obj).SayBye(); // Bye B

you can also get the LinqPad script at Test inheritence and extension methods.linq

Aman B
  • 2,276
  • 1
  • 18
  • 26
  • I understand the difference between two interfaces. My curiosity is more around compile time resolution of extension methods when class implements both the interfaces and extension method is defined for both of them – rahulaga-msft Jan 29 '18 at 08:49