如何允许用户选择如何使用变量对列表进行排序? [英] how to allow the user to choose how to sort his list using a variable?

查看:185
本文介绍了如何允许用户选择如何使用变量对列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户输入的信息可以通过多种方式进行升序和降序排序,而我试图让用户选择他想如何查看数据:

the information that the user enters can be sorted in many ways ascending and descending and i'm trying to make the user choose how he want to see the data:

因此,有一种方法可以设置用户输入的变量并对数据进行排序,而无需多次重复输入代码,具体取决于输入,例如:

so is there any way to set a variable that the user enter and sort the data without repeating the code multiple time depending on the input like:

var empName = el.Select(i => new { i.ID, i.FullName });
    if(emsort.Text="text1")
    empName.OrderBy(i.text1);
else if...

通过做一些简短的事情,例如:

by doing something shorter like:

 string sort=emsort.Text ;
empName.OrderBy(sort);

推荐答案

如果问题中的 User 是某些调用您函数的软件,则用户会知道其中的对象类型el序列:他知道el序列中的元素具有哪些属性,并且知道如何对它们进行排序.

If the User in your question is some software that calls your function, then the user is aware of the type of the objects in the el sequence: he knows which properties the elements in the el sequence are, and he knows how he wants them ordered.

为什么不让该用户给您keySelector以便在OrderBy中使用?

Why not let this user give you the keySelector to use in the OrderBy?

创建扩展功能:

static class QueryableExtensions
{
    IQueryable<MyType> SelectAndSort<TSource, TResult>(this IQueryable<TSource source,
        Expression<Func<TSource, TResult>> selector, 
        Expression<Func<TSource, TKey>> sortKeySelector,
        System.ComponentModel.ListSortDirection sortDirection)
    {
        var selectBeforeOrdering = myRepository.el.Select(selector);
        // note: this is still an IQueryable, only the query has been made,
        // the database is not accessed yet!

        IQueryable<TResult> result = (sortDirection == ListSortDirectrion.Ascending) ?
            // sort in ascending order:
            selectBeforeOrdering.OrderBy(sortKeySelector) :
            // else: selec in descending order:
            selectBeforeOrdering.OrderByDescending(sortKeySelector);

        return result;
    }
}

用法:假设您的用户知道您的el,并且他想要它的几个字段,这些字段按el

Usage: suppose your user knows your el, and he want several fields of it, ordered by one of the properties of your el

using (var myDbContext = new DbContext(...))
{
    IQueryable myEls = myDbContext.el;

    var mySelectedItems = myEls.SelectAndSore(
        // selector: what properties do I as a user want?
        el => new
        {
             Id = el.Id,
             FullName = el.FullName,
             ... // other desired properties
        },
        // sortSelector: how do I want my selected items to be sorted?
        selectedItem => selectedItem.FullName,
        // direction:
        ListSortDirection.Descending);

        // note: until now the database has not been accessed
        // only the Expression of the query has been created.

        // depending on what you want as a result: ToList / ToArray / First / ...
        return mySelectedItems.ToList();
    }

另一方面,如果您的用户不是软件,而是操作员,而该操作员必须选择要订购其商品的列,则他必须具有一种方法来告诉计算机应该对哪个列进行排序.

If, on the other hand your user is not software, but an operator, who has to select by which column he wants his items to be ordered, he has to have a method to tell the computer which column should be sorted.

这通常是通过单击列来完成的.另一种方法可以是在组合框中选择一个值.无论如何,您必须将某些东西附加到包含sortKeySelector的列或组合框值上:

This is usually done by clicking the column. Another method could be by selecting a value in a combo box. Anyway, you'll have to attach something to the column or to the combobox values that holds the sortKeySelector:

class MySortableColumn<TDisplayedProperty> : DataGridViewColumn 
   // or whatever column class you are using
{
    public Expression<Func<MyDisplayedItem, TDisplayedProperty>> SortKeySelector{get;set;}
}

现在您有几列,每列显示MyDisplayedItem的属性之一:

Now you have several columns that each display one of the properties of MyDisplayedItem:

var columnId = new MySortableColumn<int>()
{
    SortKeySelector = displayedItem => myDisplayedItem.Id,
};
var columnFullName = new MyStortableColumn<string>()
{
    SortKeySelector = displayedItem => myDisplayedItem.Id,
}
// etc.

每当操作员单击列时,都会提取sortKey并将其用作对项目进行排序的参数:

Whenever the operator clicks on a column, the sortKey is extracted and used as parameter to sort the items:

void SortColumn(SortableColumn column)
{
     var sortedItems = Sort(column.SortKeySelector, ListSortOrder...);
     Display(sortedItems);
}

这篇关于如何允许用户选择如何使用变量对列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆