6
I’m performing some tests and managed to implement using this form:
public class Residencia
{
public object Id
{
get { return String.Concat(Cidade, Estado); }
set { }
}
private string Cidade;
private string Estado;
public Residencia(string cidade, string estado)
{
Cidade = cidade;
Estado = estado;
}
}
private static void Main(string[] args)
{
List<Residencia> ListaResidencia = new List<Residencia>();
ListaResidencia.Add(new Residencia("São Paulo", "SP"));
ListaResidencia.Add(new Residencia("Bauru", "SP"));
var cidade = new Residencia("Bauru", "SP");
if (ListaResidencia.Any(x => x.Id.Equals(cidade.Id)))
{
Console.Write("Já existe uma Residência com esses dados.");
}
else
{
ListaResidencia.Add(cidade);
}
ListaResidencia.Count();
Console.ReadKey();
}
My question is if this form I implemented is the correct one (follows the programming patterns) and if there is another way to do this (improve).
Very good Miguel! Your knowledge in C# is excellent, thank you.
– Laerte
To legitimize the answer here is the explanation on composite key following the MSDN: http://msdn.microsoft.com/pt-br/library/bb907099.aspx
– MayogaX
@Mayogax: the use of an instance of an Anonimous-type is also a great option because the
Equals
of Anonimous-type is implemented by comparing all fields. Thank you for the additional information. = D– Miguel Angelo