Order linq request

For my general grid, I am currently doing this to activate sorting:

Elements.OrderBy(column.SortExpression).AsQueryable();

In which SortExpression is a type Func<T, object>and the column is a generic classColumn<T>

I installed SortExpression in the controller as follows:

new Column<OrderViewData>{Key = "ShippingDate", SortExpression = e => e.ShippingDate}

"OrderBy" invokes the sql statement that I do not want.

So, I'm trying to replace it with this:

Elements = from element in Elements
           orderby column.SortExpression
           select element;

Which does not cause sql execution.

Now, the forcourse.SortExpression column should be of a different type. Only I can not understand what type it should be, and how to set it to a common class in the controller.

I can still install SortExpression in a generic style that is robust.

Any suggestions on how I can sort by an expression set somewhere else in the application, without executing sql when applying the order to IQueryable?

@Earwicker:

:

Expression<Func<Employee, DateTime?>> sortexpression = e => e.BirthDate;
var db = new NorthwindDataContext();
var query = from e in db.Employees
            select e;
query = query.OrderBy(sortexpression);
int count = query.Count();

:

SELECT COUNT(*) AS [value]
FROM [dbo].[Employees] AS [t0]

DateTime? :

Expression<Func<Employee, object>> sortexpression = e => e.BirthDate;

InvalidOperationException: 'System.Object'

: " DateTime?", , . , Expression<Func<Employee, some_type>>. , - , SortExpression = e => e.BirthDate, "T".

, - , - e.BirthDate, Func<T, object> Expression<Func<T,some_type>>? - , : Elements.OrderBy(column.SortExpression.FixCast())

, . SQL- , .

, !

@earwicker 2:

var gridBuilder = new RainbowGridBuilder<Employee>("Examples_Employees")
{
    Elements = GetEmployees(), //The elements (records) we will show in our grid. 

    //Define the columns for our grid.
    Columns = new List<Column<Employee>>{
            new Column<Employee> {
                Key = "EmployeeId",                             //Key is used for sorting, selecting columns, ...
                Header = "Employee Id",                         //The header of the column. Also used as caption in the column selection checkboxlist.
                ValueExpression = e => e.EmployeeID.ToString(), //The Linq expression which will be executed on each element to fill the cell
                SortExpression = e => e.EmployeeID,             //The Linq expression by which to sort the elements in the grid. 
                Display = false},                               //Is this column visible by default?
            new Column<Employee> {Key = "Name", ValueExpression = e => e.FirstName + " " + e.LastName, SortExpression = e => e.LastName},
        },

    // Some other properties here that are irrelevant.


}
+3
4

, , .

: :

Columns = new List<Column<Employee>>{
            new Column<Employee> {
                Key = "EmployeeId",   
                SortExpression = new SortExpression<Employee>(e => e.EmployeeID)
                // ... other irrelevant column properties ...  
            },
            new Column<Employee> {
                Key = "EmployeeBirthDate",      
                SortExpression = new SortExpression<Employee>(e => e.BirthDate)
            }
          };

buildGrid() RainbowGridBuilder :

if (columnToSort != null) //sort the elements according to the set column key and the sortexpression
{
    var column = Columns.Where(c => c.Key == columnToSort).SingleOrDefault();
    if (column != null)
    {
        Elements = column.SortExpression.ApplySortExpression(Elements, descending);
    }
}

:

SortExpression, , Expression<Func<T, some_specific_Type>>.

SortExpression ApplySortExpression, :

public class SortExpression<T>
{
    private Expression<Func<T, DateTime?>> DateTimeExpression { get; set; }
    private Expression<Func<T, int?>> intExpression { get; set; }
    private Expression<Func<T, string>> stringExpression { get; set; }

    public SortExpression(Expression<Func<T, DateTime?>> expression)
    {
        DateTimeExpression = expression;
    }

    public SortExpression(Expression<Func<T, int?>> expression)
    {
        intExpression = expression;
    }

    public SortExpression(Expression<Func<T, string>> expression)
    {
        stringExpression = expression;
    }

    public IQueryable<T> ApplySortExpression(IQueryable<T> elements, bool? descending)
    {
        if (DateTimeExpression != null)
        {
            if (descending.HasValue && descending.Value)
                return elements.OrderByDescending(DateTimeExpression);
            else
                return elements.OrderBy(DateTimeExpression);
        }
        else if (intExpression != null)
        {
            if (descending.HasValue && descending.Value)
                return elements.OrderByDescending(intExpression);
            else
                return elements.OrderBy(intExpression);
        }
        else if (stringExpression != null)
        {
            if (descending.HasValue && descending.Value)
                return elements.OrderByDescending(stringExpression);
            else
                return elements.OrderBy(stringExpression);
        }
        else
            throw new Exception("Unsuported sortkey type");
    }
}

, , . , "new SortExpression<Employee>".

+1

SortExpression Expression<Func<T, object>>.

Func<T, object>, IL, . Linq to SQL ( NHibernate, - ) , , SQL - . lambda Expression<Func<...>>, .

, ?

Elements = Elements.OrderBy(e => e.ShippingDate);

?

Expression<Func<OrderViewData, object>> sortExpression = e => e.ShippingDate;
Elements = Elements.OrderBy(sortExpression);

, object.

, , :

new Column<OrderViewData>{Key = "ShippingDate", SortExpression = e => e.ShippingDate}

, Column TElem ( , ) TSort ( ).

new Column<Employee, DateTime?> { SortExpression = e => e.BirthDate }

, SortExpression :

Expression<Func<TElem, TSort>> SortExpression { get; set; }
+3

, , OrderBy Queryable. IEnumerable <T> , - . IQueryable <T> , IEnumerable <T> ... . , , IQueryable . , , .., .

0

orderby:

        var SampleData = new SampleDataContext();

        var text = from i in SampleData.Events
                   select i;
        var text2 = text.OrderByDescending(i => i.id);
        return View(text2);
0
source

All Articles