C#如何根据另一个列表的顺序对列表进行排序

时间:2019-07-08 20:37:59

标签: c# list linq sorting entity-framework-6

如何根据另一个列表中的字符串顺序对IEnumerable / List排序?

已从数据库(使用EF6)提取数据以填充可变项。现在,我需要根据在列表/数组中对字符串进行索引的顺序对它进行排序。

这就是我所拥有的

var datasource = context.Get_The_Info_That_I_Need();
List<string> sortOrder = new List<string> { "Pending", "Ready For Pickup", "Checked Out" };

var source = datasource.ToList().OrderBy(s => sortOrder.FindIndex(x => x.Equals(s.Status.Name, StringComparison.OrdinalIgnoreCase)))
                                .ThenBy(s => s.DateRequested);

GridView1.DataSource = source;
GridView1.DataBind();

我遇到的问题是列表未按状态排序。我需要首先按日期对所有“待处理”项进行排序;然后按日期对“ RFP”进行排序;最后是“ CO”按日期排序。

我从this SO question获得了这段代码。

2 个答案:

答案 0 :(得分:1)

我建议 完成datasource.ToList()之后 尝试调用此函数:

 private static List<Flight> SortListByOtherList(List<Flight> UnSortedList, List<string> SortKeys)
        {
            //replace 'object' with your flight class name
            List<Flight> SortedList = new List<Flight>();
            foreach (string Key in SortKeys)
            {
                SortedList.AddRange((from Flight in UnSortedList
                              //Here add the 'get' command of your string Time.Date instand of 'Time'
                              orderby Flight.GetTime() descending
                              //Here add the 'get' command of your string flight stutus instand of 'FlightStutus'
                              where Flight.GetFlightStutus() == Key
                             select Flight).ToList());

            }
            return SortedList;
        }

如果您只想根据我的代码执行以下操作,则为完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace ConsoleApp6
{
    public class Flight
    {
        string FlightStutus { get; set; }
        DateTime Time { get; set; }
        public Flight(string FlightStutus, DateTime Time)
        {
            this.Time = Time;
            this.FlightStutus = FlightStutus;
        }
        public DateTime GetTime()
        {
            return this.Time;
        }
        public string GetFlightStutus()
        {
            return this.FlightStutus;
        }
        public override string ToString()
        {
            return $"FlightStutus: {this.FlightStutus} Time: {this.Time}";
        }

    }
    class Program
    {
        static void Main(string[] args)
        {

            List<string> sortOrder = new List<string> { "Pending", "Ready For Pickup", "Checked Out" };
            List<Flight> ListThatNeedToGetSorted = new List<Flight>();
            ListThatNeedToGetSorted.Add(new Flight("Pending", new DateTime(2005, 12, 12, 9, 0, 0,5)));
            ListThatNeedToGetSorted.Add(new Flight("Ready For Pickup", new DateTime(2005, 12, 12, 9, 0, 0,7)));
            ListThatNeedToGetSorted.Add(new Flight("Checked Out", new DateTime(2005, 12, 12, 9, 0, 0,5)));
            ListThatNeedToGetSorted.Add(new Flight("Checked Out", new DateTime(2012, 12, 10, 9,5, 0)));
            ListThatNeedToGetSorted.Add(new Flight("Pending", new DateTime(2012, 4, 2, 11, 4, 22)));
            List<Flight> SortedList=SortListByOtherList(ListThatNeedToGetSorted, sortOrder);
            foreach (Flight Fl in SortedList)
            {
              Console.WriteLine(Fl);
            }
        }

        private static List<Flight> SortListByOtherList(List<Flight> UnSortedList, List<string> SortKeys)
        {
            //replace 'object' with your flight class name
            List<Flight> SortedList = new List<Flight>();
            foreach (string Key in SortKeys)
            {
                SortedList.AddRange((from Flight in UnSortedList
                              //Here add the 'get' command of your string Time.Date instand of 'Time'
                              orderby Flight.GetTime() descending
                              //Here add the 'get' command of your string flight stutus instand of 'FlightStutus'
                              where Flight.GetFlightStutus() == Key
                             select Flight).ToList());

            }
            return SortedList;
        }
        }
    }

答案 1 :(得分:0)

所以我的方法有效。我做了一个批判的人造假。我忘记将数据源设置为新的源变量。该方法确实可以按照原始问题下的注释中的说明起作用。