C# LINQ如何去Distinct物件List透過物件某個屬性

 






  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class WIP_StationStatusModel
    {
        public string Id { get; set; }

        public string Wo { get; set; }

        public string Line { get; set; }

        public string Station { get; set; }

        public string Status { get; set; }

        public DateTime TranslationTime { get; set; }

        public string Ip { get; set; }

        public string JigNo { get; set; }

        public string Creator { get; set; }

        public DateTime? CreateDate { get; set; }

    }

    class Program
    {
        static void Main(string[] args)
        {
            List<WIP_StationStatusModel> StationStatusDatas = new List<WIP_StationStatusModel>();
            for (int idx = 0; idx < 8; idx++)
            {
                WIP_StationStatusModel wIP_StationStatusModel = new WIP_StationStatusModel();
                if (new List<int>() { 1, 5, 6 }.Contains(idx))
                {
                    wIP_StationStatusModel.Id = "1";
                }
                else if (new List<int>() { 2, 7 }.Contains(idx))
                {
                    wIP_StationStatusModel.Id = "2";
                }
                else
                {
                    wIP_StationStatusModel.Id = "3";
                }
                wIP_StationStatusModel.Wo = string.Format("WO_{0}", idx);
                wIP_StationStatusModel.Line = string.Format("LINE_{0}", idx);
                wIP_StationStatusModel.Station = string.Format("Station_{0}", idx);
                if (idx % 2 == 1 || idx == 4)
                {
                    wIP_StationStatusModel.Status = "START";
                }
                else
                {
                    wIP_StationStatusModel.Status = "END";
                }

                if (idx == 7 || idx == 8)
                {
                    wIP_StationStatusModel.Wo = "BB";
                }
                else
                {
                    wIP_StationStatusModel.Wo = "AA";
                }
                double mins = 30 * idx;
                DateTime dt = DateTime.Now.AddMinutes(mins);
                wIP_StationStatusModel.TranslationTime = dt;
                wIP_StationStatusModel.Ip = "";
                StationStatusDatas.Add(wIP_StationStatusModel);
            }
            Console.WriteLine("Method1.");

            List<WIP_StationStatusModel> distincStationList = StationStatusDatas.GroupBy(x => x.Id).Select(y => y.First()).ToList();
            foreach (WIP_StationStatusModel obj in distincStationList)
            {
                Console.WriteLine($"{ string.Format("{0}-{1}", obj.Station, obj.Line) } id: {obj.Id}");
            }


            Console.WriteLine("Method2.");

            var distincStationList2 = StationStatusDatas.Distinct(new StationComparer());
            foreach(WIP_StationStatusModel obj in distincStationList2)
            {
                Console.WriteLine($"{ string.Format("{0}-{1}",obj.Station,obj.Line) } id: {obj.Id}");
            }

            Console.ReadKey();
        }
    }

    public class StationComparer : IEqualityComparer<WIP_StationStatusModel>
    {
        public bool Equals(WIP_StationStatusModel obj1, WIP_StationStatusModel obj2)
        {
            return obj1?.Id == obj2?.Id;
        }


        public int GetHashCode(WIP_StationStatusModel obj)
        {
            return obj.Id.GetHashCode();
        }
    }


}



Ref:

https://makolyte.com/csharp-select-distinct-objects-based-on-a-property/












留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題