Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are not trying to extend <code>IQueryable</code> at all by the look of your example. Remove <code>this</code> from method definition and your example should work fine.</p> <pre><code>public class OrderByCollection { // .. shameless cut .. public IQueryable&lt;T&gt; ExecuteOrderBys&lt;T&gt;(IQueryable&lt;T&gt; source) { // .. we don't need no stinking body .. } } </code></pre> <p>Which will make your example work:</p> <pre><code>var q = db.tblJobHeaders; OrderByCollection OBys = new OrderByCollection(); OBys.AddOrderBy("some field", true); OBys.AddOrderBy("anotherfield", false); OBys.ExecuteOrderBys(q); </code></pre> <p>As a side note I would not use <code>bool</code> to define how the orderby's should be sorted. The code is unclear. Use a enum or different methods instead:</p> <pre><code>OBys.AddOrderBy("some field", Sort.Ascending); </code></pre> <p>or:</p> <pre><code>OBys.AddOrderByDescending("some field"); </code></pre> <h2>Update</h2> <p>Extension methods are used to "plugin" methods to existing classes or interfaces. By writing <code>public IQueryable&lt;T&gt; ExecuteOrderBys&lt;T&gt;(this IQueryable&lt;T&gt; source)</code> you are really saying that the method should be hooked into <code>IQueryable&lt;T&gt;</code>. And it should therefore be accessed like <code>myQuery.ExecuteOrderBys</code>.</p> <p>My guess is that extension methods must be static and be included in static classes to avoid confusion:</p> <p>a) They are really not members of the classes or interfaces and can not access anything else than public fields/properties/methods. </p> <p>b) They can extend any class. Without the restriction you could put a method call <code>DoSomething(this ThatObject instance)</code> in a class called <code>ThatObject</code>. The problem with would be that you can't access anything else than the public interface since it's an extension method. </p> <p>The confusing possibilities would be endless ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload