寻找整数键控模型集合的优雅/高效解决方案

时间:2017-02-09 08:44:55

标签: c# linq generics design-patterns collections

“AddToLeg”方法看起来很长,是否有更好的模式或更有效的方法来实现它?我想过使用字典,但我希望密钥保持整数。我对linq / generics很新,所以我可能会错过一些更明显的东西。当我查看文档时,实际上没有任何示例符合我的场景。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            var model = new TrainspotterItenaryViewModel();

            var manchester = new Station() { Name = "Manchester", ExpectedTime = "13:30" };
            var leeds = new Station() { Name = "Leeds", ExpectedTime = "15:00" };
            var york = new Station() { Name = "York", ExpectedTime = "15:30" };
            var london = new Station() { Name = "London", ExpectedTime = "21:00" };

            model.AddToLeg(1, manchester);
            model.AddToLeg(1, leeds);

            model.AddToLeg(2, leeds);
            model.AddToLeg(2, london);
            model.AddToLeg(1, york); //another destination added to leg 1

            //any number of legs can be added...
            model.AddToLeg(3, manchester);

            //show results contents
            for(var i=1; i <= model.Legs.Count; i++)
            {
                var displayLeg = model.Legs.Single(x=>x.LegNumber==i);
                foreach(var station in displayLeg.Stations){
                    string output = $"leg: {displayLeg.LegNumber} station: {station.Name}, expected:{station.ExpectedTime}";
                        Console.WriteLine(output);  
                }
            }
        }
    }

    public class TrainspotterItenaryViewModel
    {
        public List<Leg> Legs { get; set; }

        public void AddToLeg(int legNumber, Station station)
        {
            if (Legs == null)
            {
                Legs = new List<Leg>();
            }

            var legCount = Legs.Count(x => x.LegNumber == legNumber);
            if (legCount == 0)
            {
                var leg = new Leg
                {
                    LegNumber = legNumber,
                    Stations = new List<Station> {station}
                };

                Legs.Add(leg);

                Console.WriteLine($"Leg {leg.LegNumber} Not Found- Added new leg and {station.Name}");
            }
            else
            {
                foreach (var leg in Legs)
                {
                    if (leg.LegNumber == legNumber)
                    {
                        leg.Stations.Add(station);
                        Console.WriteLine($"Leg {legNumber} Found- adding {station.Name}");
                    }
                }
            }
        }
    }

    public class Leg
    {
        public int LegNumber { get; set; }
        public List<Station> Stations { get; set; }
    }

    public class Station
    {
        public string Name { get; set; }
        public string ExpectedTime { get; set; }
    }
}

2 个答案:

答案 0 :(得分:3)

使用字典执行此任务:

    public class TrainspotterItenaryViewModel
    {
        private Dictionary<int, Leg> _legNumberToLegIndex { get; set; }

        public IEnumerable<Leg> Legs => _legNumberToLegIndex?.Values

        public void AddToLeg(int legNumber, Station station)
        {
            if (_legNumberToLegIndex == null)
            {
                _legNumberToLegIndex = new Dictionary<int, Leg>();
            }

            Leg leg;
            if (!_legNumberToLegIndex.TryGetValue(legNumber, out leg))
            {
                leg = new Leg
                {
                    LegNumber = legNumber,
                    Stations = new List<Station>()
                };
                _legNumberToLegIndex.Add(legNumber, leg);
            }
            leg.Stations.Add(station);
        }
    }

答案 1 :(得分:1)

方法FirstOrDefault已经返回您正在搜索的对象,因此您可以简化代码

    public List<Leg> Legs { get; set; } = new List<Leg>();

    public void AddToLeg(int legNumber, Station station)
    {
        var leg = Legs.FirstOrDefault(x => x.LegNumber == legNumber);
        if (leg == null)
        {
            leg = new Leg
            {
                LegNumber = legNumber,
                Stations = new List<Station> { station }
            };

            Legs.Add(leg);

            Console.WriteLine($"Leg {leg.LegNumber} Not Found- Added new leg and {station.Name}");
        }
        else
        {
            leg.Stations.Add(station);
            Console.WriteLine($"Leg {legNumber} Found- adding {station.Name}");
        }
    }