【问题标题】:LINQ Query with multiple conditions and setting values into a list具有多个条件并将值设置为列表的 LINQ 查询
【发布时间】:2018-05-04 00:07:49
【问题描述】:

我目前正在处理一个项目,我正在使用 xml c# 和行查询。

此函数的主要目的是创建一个日期列表,其中包含每个日期(跨所有驱动程序)覆盖的总距离,按日期排序(请注意,日期格式为 YYYY/MM/YY,以便于排序)

我唯一不确定的部分是我按日期排序的最后一部分,因为我目前只能按距离排序。

public List<String> CalculateDistDates()
    {
        List<String> distDatesList = new List<string>();

        var datesDistTot =
            from Driver in this.Drivers
            from Journey in Driver.Journeys
            group Journey by Journey.JourneyDate into distance
            let totaldistance = (from jour in distance
            select (int)jour.Distance).Sum() 
            orderby totaldistance descending


            select new
            {
                journeyDate = .Key,
                totaldistance = totaldistance

            };

【问题讨论】:

  • 我正在查找 xml 中每个日期的总距离,来自司机的行程。
  • 您可以按多个字段排序,用逗号分隔。

标签: c# xml linq


【解决方案1】:

试图回答您的问题并稍微调整代码以达到相同的结果。

模拟模型

 public class Journey
    {
        public decimal Distance { get; set; }
        public DateTime JourneyDate { get; set; }
    }
    public class Drivers
    {
        public string Name { get; set; }
        public Journey Journey { get; set; }
    }

    public class  DistanceCovered
    {
        public DateTime JourneyDate { get; set; }
        public decimal TotalDistance { get; set; }
    }

这是在控制台应用程序中创建的函数,只需删除测试代码并用您的数据填充它。

public static void CalculateDistDates()
    {            
        var drivers = new List<Drivers>
        {
            new Drivers{Journey = new Journey {Distance = 1.2M, JourneyDate = DateTime.Now.AddDays(1).Date}},
            new Drivers{Journey =  new Journey{Distance = 1.2M, JourneyDate = DateTime.Now.AddDays(2).Date}},
            new Drivers{Journey =  new Journey{Distance = 1.1M, JourneyDate = DateTime.Now.AddDays(2).Date}},
            new Drivers{Journey =  new Journey{Distance = 1.5M, JourneyDate = DateTime.Now.AddDays(3).Date}},
            new Drivers{Journey = new Journey {Distance = 1.7M, JourneyDate = DateTime.Now.AddDays(-1).Date}},
            new Drivers{Journey =  new Journey{Distance = 1.1M, JourneyDate = DateTime.Now.AddDays(3).Date}},
            new Drivers{Journey =  new Journey{Distance = 0.2M, JourneyDate = DateTime.Now.AddDays(2).Date}}
        };

        var totalDistance = (from d in drivers
            group d by d.Journey.JourneyDate
            into j                 
            select new DistanceCovered
            {
                JourneyDate = j.Key,
                TotalDistance = j.Sum(r=>r.Journey.Distance)
            }).ToList().OrderBy(r=>r.JourneyDate).ThenByDescending(r=>r.TotalDistance);

        foreach (var t in totalDistance)
        {
            Console.WriteLine($"{t.JourneyDate}: {t.TotalDistance}" );
        }

        Console.ReadLine();            
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2016-05-28
    • 2011-09-21
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多