加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – System.Linq.Expressions.Expression for .OrderBy函数

发布时间:2020-12-15 21:40:12 所属栏目:百科 来源:网络整理
导读:我的代码的行部分是重复的. 您将看到下面的lambda表达式重复4次 x = x.GetValuestring("City")).ThenByDescending(x = HelperFunctions.strToTZDateTime(x.GetValuestring("EventStartStr"),x.GetValuestring[]("EventTimeZone")[0].ToString()) 代码示例:
我的代码的行部分是重复的.

您将看到下面的lambda表达式重复4次

x => x.GetValue<string>("City")).ThenByDescending(x => HelperFunctions.strToTZDateTime(x.GetValue<string>("EventStartStr"),x.GetValue<string[]>("EventTimeZone")[0].ToString())

代码示例:

//SORT DATA
    switch (SortDetails)
    {
        case ("Date"):
        default:
            if (SortOrder == "ASC")
            {
                _allEvents = _allEvents.OrderBy(x => HelperFunctions.strToTZDateTime(x.GetValue<string>("EventStartStr"),x.GetValue<string[]>("EventTimeZone")[0].ToString()));
            }
            else
            {
                _allEvents = _allEvents.OrderByDescending(x => HelperFunctions.strToTZDateTime(x.GetValue<string>("EventStartStr"),x.GetValue<string[]>("EventTimeZone")[0].ToString()));
            }
            break;
        case ("Location"):
            if (SortOrder == "ASC")
            {
                _allEvents = _allEvents.OrderBy(x => x.GetValue<string>("Country")).ThenBy(x => x.GetValue<string>("ProvinceState")).ThenBy(x => x.GetValue<string>("City")).ThenBy(x => HelperFunctions.strToTZDateTime(x.GetValue<string>("EventStartStr"),x.GetValue<string[]>("EventTimeZone")[0].ToString()));
            }
            else
            {
                _allEvents = _allEvents.OrderByDescending(x => x.GetValue<string>("Country")).ThenByDescending(x => x.GetValue<string>("ProvinceState")).ThenByDescending(x => x.GetValue<string>("City")).ThenByDescending(x => HelperFunctions.strToTZDateTime(x.GetValue<string>("EventStartStr"),x.GetValue<string[]>("EventTimeZone")[0].ToString()));
            }
            break;
    }

我想采取第一行,并使其成为我可以进入OrderBy的东西.

就像是 :

System.Linq.Expressions.Expression<Func<DynamicContent,DateTime>> sortLambda = x => HelperFunctions.strToTZDateTime(x.GetValue<string>("EventStartStr"),x.GetValue<string[]>("EventTimeZone")[0].ToString());

进入:

allEvents = _allEvents.OrderBy(sortLambda);

不幸的是,这似乎并没有奏效.

解决方法

这个控制台应用程序应该修复你. sortLamda可以是任何类型,希望能回答有关动态内容的问题.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

internal class BoringEvent
{
    public string EventStartStr { get; set; }
    public string EventTimeZone { get; set; }
    public string ProvinceState { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

internal enum SortedBy
{
    Ascending = 0,Descending
}

class ExpressionsTest
{
    internal static List<BoringEvent> _allEvents = new List<BoringEvent>();
    internal static class HelperFunctions
    {
        public static DateTime strToTZDateTime(string startDate,string timeZone)
        {
            //I'm too lazy to figure out dates by time zone.
            //Your function already has the logic,so why bother.
            //Let's assume the time zone is equal in this test case
            return DateTime.Parse(startDate);
        }
    }

    internal static void Main()
    {

        _allEvents.Add(new BoringEvent {
            EventStartStr = "12/31/1999",//let's party like it's 1999 - (yawn) I'm stuck at this event :< 
            EventTimeZone = "en-us",City = "Philadelphia",ProvinceState = "Pennsylvania",Country = "United States of America"});
        _allEvents.Add(new BoringEvent
        {
            EventStartStr = "01/01/1970",EventTimeZone = "en-us",City = "New York",ProvinceState = "New York",Country = "United States of America"
        });
        IEnumerable<BoringEvent> sortedEvents = null;
        //sort by date
        Console.WriteLine("Sorting Events by Ascending date...");
        Expression<Func<BoringEvent,DateTime>> sortLamba = evt => HelperFunctions.strToTZDateTime(evt.EventStartStr,evt.EventTimeZone);
        sortedEvents = _allEvents.SortEvents(SortedBy.Ascending,sortLamba);
        Print(sortedEvents);
        //sort by country,then state,then city,then date
        Console.WriteLine("Sorting Events Desc by Country,then ProvinceState,then City,then Date");
        Expression<Func<BoringEvent,object>>[] sortLamba2 = new Expression<Func<BoringEvent,object>>[] 
        {
            evt => evt.Country,evt => evt.ProvinceState,evt => evt.City,evt => HelperFunctions.strToTZDateTime(evt.EventStartStr,evt.EventTimeZone)
        };
        sortedEvents = _allEvents.SortEvents(SortedBy.Descending,sortLamba2);   
        Print(sortedEvents);

        Console.Read();
    }

    private static void Print(IEnumerable<BoringEvent> events)
    {
        for(int i = 0; i < events.Count(); i++)
        {
            BoringEvent evt = events.ElementAt(i);
            Console.WriteLine("Event: {0}",i.ToString());
            Console.WriteLine("tEventStartStr: {0}",evt.EventStartStr);
            Console.WriteLine("tEventTimeZone: {0}",evt.EventTimeZone);
            Console.WriteLine("tCity: {0}",evt.City);
            Console.WriteLine("tProvinceState: {0}",evt.ProvinceState);
            Console.WriteLine("tCountry: {0}",evt.Country);
        }
    }
}

internal static class EventExtensions
{

    public static IEnumerable<TResult> SortEvents<TResult,T>(
        this IEnumerable<TResult> events,SortedBy sortByType,params Expression<Func<TResult,T>>[] expressions)
    {
        IEnumerable<TResult> retVal = null;
        switch(sortByType)
        {
            case SortedBy.Ascending:
                retVal = EventExtensions.SortEventsAsc(events,expressions);
                break;
            case SortedBy.Descending:
                retVal = EventExtensions.SortEventsDesc(events,expressions);
                break;
            default:
                throw new InvalidOperationException(
                    string.Format("The SortedBy enumeration does not contain a case for the value of '{0}'.",Enum.GetName(typeof(SortedBy),sortByType)));
        }
        return retVal;
    }

    public static IEnumerable<TResult> SortEventsAsc<TResult,T>>[] expressions)
    {
        IOrderedEnumerable<TResult> sorted = null;
        for(int i = 0; i < expressions.Count(); i++)
        {
            Expression<Func<TResult,T>> exp = 
                (Expression<Func<TResult,T>>)expressions[i];
            Func<TResult,T> deleg = exp.Compile();
            if(i == 0)
            {
                sorted = events.OrderBy(evt => deleg.Invoke(evt));
            }
            else
            {
                sorted = sorted.ThenBy(evt => deleg.Invoke(evt));
            }
        }
        return sorted;
    }

    public static IEnumerable<TResult> SortEventsDesc<TResult,T>>[] expressions)
    {
        IOrderedEnumerable<TResult> sorted = null;
        for (int i = 0; i < expressions.Count(); i++)
        {
            Expression<Func<TResult,T> deleg = exp.Compile();
            if (i == 0)
            {
                sorted = events.OrderByDescending(evt => deleg.Invoke(evt));
            }
            else
            {
                sorted = sorted.ThenByDescending(evt => deleg.Invoke(evt));
            }
        }
        return sorted;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读