Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat does the error "Extension method must be static" mean?
    primarykey
    data
    text
    <p>I'm having trouble with this class, in particular the method:</p> <pre><code>public IQueryable&lt;T&gt; ExecuteOrderBys&lt;T&gt;(this IQueryable&lt;T&gt; source) </code></pre> <p>It says the error:</p> <blockquote> <p>Extension method must be static</p> </blockquote> <p>However when I made the method static, it throws other areas, specifically to <code>this.xxx</code> can't be accessed in a static method.</p> <p>I'm a bit confused about the return types with <code>&lt;T&gt;</code> and the return types, if someone could explain it to me and how it works I would be grateful.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Collections; /// &lt;summary&gt; /// A collection of order bys /// &lt;/summary&gt; public class OrderByCollection { private ArrayList Orderings = new ArrayList(); public int? Skip { get; set; } public int? Take { get; set; } public OrderByCollection() { // Default skip and takes to nulls so we know if they are set or not this.Skip = null; this.Take = null; } /// &lt;summary&gt; /// Add an order by to this collection /// &lt;/summary&gt; public void AddOrderBy(string Field, bool Descending) { OrderByObj NewObj = new OrderByObj(Descending, Field); this.Orderings.Add(NewObj); } /// &lt;summary&gt; /// Executes the order bys /// &lt;/summary&gt; public IQueryable&lt;T&gt; ExecuteOrderBys&lt;T&gt;(this IQueryable&lt;T&gt; source) { int ExecutionIndex = 0; foreach (OrderByObj O in this.Orderings) { if (ExecutionIndex == 0) { if (O.Descending) source = LinqHelper.OrderByDescending(source, O.Field); else source = LinqHelper.OrderBy(source, O.Field); } else { if (O.Descending) source = LinqHelper.ThenByDescending((IOrderedQueryable&lt;T&gt;)source, O.Field); else source = LinqHelper.ThenBy((IOrderedQueryable&lt;T&gt;)source, O.Field); } ExecutionIndex++; } // Skip and take if (this.Skip != null) source = source.Skip(this.Skip.Value); if (this.Take != null) source = source.Take(this.Take.Value); return (IOrderedQueryable&lt;T&gt;)source; } } </code></pre> <p><strong>Edit</strong></p> <p>I'm trying to make a class that can do the following:</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>
    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.
 

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