简体   繁体   English

比较几个对象并检查是否具有相同的值

[英]Compare few object and check are have same value

I would like to know if the lens does not repeat or the user did not give the same values. 我想知道镜头是否不重复或用户未提供相同的值。

It is my Ticket class : 这是我的机票舱位:

   public class TicketBl
    {
        public int Id { get; set; }
        public string UserEmail { get; set; }
        public int CinemaId { get; set; }
        public int MovieId { get; set; }
        public int CinemaHallId { get; set; }
        public string Row { get; set; }
        public int Seat { get; set; }
        public DateTime TimeOfSeance { get; set; }
        public int Price { get; set; }
    }

And I have list of tickets : 我有门票清单:

var tickets = new List<TicketBl>
            {
                new TicketBl()
                {
                    CinemaHallId = 1, MovieId = 1, Row = "A", Seat = 1, CinemaId = 1,
                    TimeOfSeance = new DateTime(2019, 05, 06), UserEmail = "bk@gmail.com"
                },
                new TicketBl()
                {
                    CinemaHallId = 1, MovieId = 1, Row = "A", Seat = 2, CinemaId = 1,
                    TimeOfSeance = new DateTime(2019, 05, 06), UserEmail = "bk@gmail.com@gmail.com"
                },
                new TicketBl()
                {
                    CinemaHallId = 1, MovieId = 1, Row = "A", Seat = 1, CinemaId = 1,
                    TimeOfSeance = new DateTime(2019, 05, 06), UserEmail = "bk@gmail.com@gmail.com"
                }
            };

I created some value comparer class but it's not working. 我创建了一些值比较器类,但是它不起作用。 Equals isn't good for comparer values ?? 等于不利于比较器值? How to check the values in object is the same. 如何检查对象中的值相同。

public static class ValuesComparer
    {
        public static bool CheckListHasSameValues<T>(List<T> values)
        {
if (values.Count() == 1)
            {
                return false;
            }

            for (int i = 0; i < values.Count - 1; i++)
            {
                for (int j = i + 1; j < values.Count; j++)
                {
                    if (values[i].Equals(values[j]))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }

Compare will not compare items of different type like strings and integers. 比较不会比较不同类型的项目,例如字符串和整数。 Use IEqual : 使用IEqual:

    public class Ticket : IEquatable<Ticket>
    {
        public int Id { get; set; }
        public string UserEmail { get; set; }
        public int CinemaId { get; set; }
        public int MovieId { get; set; }
        public int CinemaHallId { get; set; }
        public string Row { get; set; }
        public int Seat { get; set; }
        public DateTime TimeOfSeance { get; set; }
        public int Price { get; set; }

        public Boolean Equals(Ticket other)
        {
            return
                (this.Id == other.Id) &&
                (this.CinemaId == other.CinemaId) &&
                (this.MovieId == other.MovieId) &&
                (this.CinemaHallId == other.CinemaHallId) &&
                (this.Row == other.Row) &&
                (this.Seat == other.Seat) &&
                (this.TimeOfSeance == other.TimeOfSeance);

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM