惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Google Online Security Blog
Google Online Security Blog
T
Threat Research - Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
NISL@THU
NISL@THU
V
Vulnerabilities – Threatpost
V2EX - 技术
V2EX - 技术
Cisco Talos Blog
Cisco Talos Blog
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
量子位
Security Archives - TechRepublic
Security Archives - TechRepublic
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
H
Hacker News: Front Page
D
Docker
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
Kaspersky official blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
小众软件
小众软件
S
Security Affairs
博客园 - 聂微东
AI
AI
Latest news
Latest news
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Engineering at Meta
Engineering at Meta
Cloudbric
Cloudbric
S
Secure Thoughts
Spread Privacy
Spread Privacy
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - 绿叶

Suspicions about the new platform How to create your own api with ACL in Magento 台服信长之野望全后台辅助外挂 SEO Elite ver 4.0 R93 Patcher Remove 5 seconds constraint in Garena(GGC) - 绿叶 Robot development framework in Mush Client (Lua) 巧用Marshal.GetDelegateForFunctionPointer--C#如何调用按键精灵插件dll Using .Net, Flex, and Red5 to create a flash web application 做了个C#的Hotkey简单封装,希望对大家有帮助 功夫世界外挂发布测试 我的第一个全后台CALL外挂-功夫世界外挂 通过修改Mutex来达到大航海OL多开 如何利用WPE对大航海Online进行港内瞬移,瞬时生产和快速进港 MonoRail PetShop Source Code Released MS PetShop3 -> MonoRail PetShop 完成移植 移植MSPetShop3到Castle MonoRail -Model与DAL层的移植(AR) 对 "闭包-closure" 的一些见解 圈内Castle文章索引 利用Castle Framework发email是如此easy
dynamic sorting expression builder
绿叶 · 2010-10-11 · via 博客园 - 绿叶

代码

 public static class Paging
    {
        
public interface ISortbyBuilder<TSource>
        {   
            IQueryable
<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc);
        }
public class SortbyBuilder<TSource, TKey> : ISortbyBuilder<TSource>
        {
            
public IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc)
            {
                var castedKeySelector 
= (Expression<Func<TSource, TKey>>) keySelector;
                
return asc ? obj.OrderBy(castedKeySelector) : obj.OrderByDescending(castedKeySelector);
            }
        }
public class SortbyBuilderCache<TSource>
        {
            
private static readonly IDictionary<Type, ISortbyBuilder<TSource>> Cache = new Dictionary<Type, ISortbyBuilder<TSource>>();public static IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc)
            {
                
// TODO: I told you this is magic!
                var selectorReturnType = keySelector.Body.Type;
                ISortbyBuilder
<TSource> sortbyBuilder;
                
if (!Cache.TryGetValue(selectorReturnType, out sortbyBuilder))
                {
                    var typeArguments 
= new[] { typeof(TSource), selectorReturnType };
                    var builderType 
= typeof(SortbyBuilder<,>).MakeGenericType(typeArguments);
                    sortbyBuilder 
= (ISortbyBuilder<TSource>)Activator.CreateInstance(builderType);
                }
                
return sortbyBuilder.SortBy(obj, keySelector, asc);
            }
        }
public static IPagination<T> AsPagination<T>(this IQueryable<T> obj, int pageNumber, int pageSize, string sortExpression)
        {
            sortExpression 
= sortExpression ?? string.Empty;
            var asc 
= !sortExpression.Contains("DESC");

            var sortKey 

= sortExpression.Split(' ')[0];if (string.IsNullOrEmpty(sortKey) || TypeDescriptor.GetProperties(typeof (T)).Find(sortKey, false== null)
            {
                
return obj.AsPagination(pageNumber, pageSize);
            }
// TODO: magic
            var parameterExpression = Expression.Parameter(typeof (T));
            var propertyExpression 
= Expression.Property(parameterExpression, sortKey);
            LambdaExpression lambdaExpression;
            
if (typeof (IEnumerationWrapper).IsAssignableFrom(propertyExpression.Type))
            {
                var valueExpression 
= Expression.Property(propertyExpression, "Value");
                lambdaExpression 
= Expression.Lambda(valueExpression, new[] {parameterExpression});
            }
            
else
            {
                lambdaExpression 
= Expression.Lambda(propertyExpression, new[] {parameterExpression});
            }
return SortbyBuilderCache<T>.SortBy(obj, lambdaExpression, asc).AsPagination(pageNumber, pageSize);

        }

public static IPagination<T> AsPagination<T, TKey>(this IQueryable<T> obj, int pageNumber, int pageSize, Expression<Func<T, TKey>> keySelector, bool asc)
        {
            
return asc ? obj.OrderBy(keySelector).AsPagination(pageNumber, pageSize) : obj.OrderByDescending(keySelector).AsPagination(pageNumber, pageSize);
        }
    }