[英]Compare few object and check are have same value
我想知道鏡頭是否不重復或用戶未提供相同的值。
這是我的機票艙位:
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; }
}
我有門票清單:
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"
}
};
我創建了一些值比較器類,但是它不起作用。 等於不利於比較器值? 如何檢查對象中的值相同。
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;
}
}
比較不會比較不同類型的項目,例如字符串和整數。 使用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.