0

データセットにフィルタリング/順序付け/ページングを実装しようとしています。検索文字列でフィルタリングし、順序を適用してから、そのセットのサブセットを選択してページにしたいと考えています。

コードは次のようになります。

IEnumerable<Manufacturer> manufacturers;

if (!String.IsNullOrEmpty(genericSearch))
{
    manufacturers = db.Manufacturers.Where(l => l.Name.Contains(genericSearch));
}

manufacturers = manufacturers.OrderBy(sortColName, sortDir, true); // this won't build. it would
// build if i put 'db.Manufacturers' before the .OrderBy but then i lose my filter. it would 
// also build if i used 'l => l.Name' as the OrderBy parameter but i only have the column name 
//as a string from the client.

manufacturers.Skip(displayStart).Take(displayLength).ToList().ForEach(rec => aaData.Add
 (rec.PropertiesToList())); // this is paging where i can use ToList()

列名を文字列として並べ替えるにはどうすればよいですか?

4

1 に答える 1

1

リフレクションを使用する可能な方法の 1 つ

   public static IEnumerable<T> Sort<T>(this IEnumerable<T> list,
            string column, SortDirection direction = SortDirection.Ascending)
   {
        Func<T, object> selector = p => p.GetType().GetProperty(column).GetValue(p, null);
        return (direction == SortDirection.Ascending
                    ? list.OrderBy(selector)
                    : list.OrderByDescending(selector)
                    );
   }
于 2013-06-22T12:49:09.570 に答える