【问题标题】:C# How to sort a list base on the order of another listC#如何根据另一个列表的顺序对列表进行排序
【发布时间】:2019-07-08 20:37:59
【问题描述】:

如何根据另一个列表中字符串的顺序对 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 得到这个代码。

【问题讨论】:

  • 请展示您的“数据源”类的外观。
  • 我尝试了一个虚拟类,它确实按照你想要的方式工作。
  • 您的OrderBy() 似乎工作正常。但是GridView1 是什么? - 并检查其排序行为/设置。也许你应该在分配给DataSource时调用source.ToList()
  • 我没有正确输入代码。我的状态是关系表,而不是类中的属性。我不知道这是否会有所作为。我已经更正了我的代码。
  • @Sach 我的课程是一个带钥匙的基本 EF 课程。被引用的类中的属性是另一个类。这是一对多的关系。

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


【解决方案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;
        }
        }
    }

【讨论】:

  • 我以前也这样工作过,但我一直在寻找一种人手不足的方法。如果这是我必须做的,那么我可以。返回的数据量不会影响性能。
【解决方案2】:

所以我的方法确实有效。我做了一个关键的失礼。我忘记将数据源设置为新的源变量。有问题的方法确实如原始问题下的 cmets 中所述起作用。

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 1970-01-01
    • 2011-03-22
    • 2018-01-03
    • 1970-01-01
    • 2022-11-16
    • 2018-09-15
    • 2023-04-01
    • 2018-11-15
    相关资源
    最近更新 更多