Obtenha o índice de um elemento em uma coleção via LINQ

Person selectedPerson = new Person{Id=45, Name=”PersonDemo”};IList<Person> personList = new List<Person>();personList.Add(new Person{Id=1, Name=”Person1″});personList.Add(new Person{Id=2, Name=”Person2″});personList.Add(new Person{Id=3, Name=”Person3″});personList.Add(selectedPerson);personList.Add(new Person{Id=4, Name=”Person4″}); personList.Add(new Person{Id=5, Name=”Person5″}); int indexOfSelectedPerson = personList.Select((item, i) => new { Item …

Continuar lendo

A Better Enumerable.Range

Pessoalmente, eu não gosto muito disso:, Enumerable.Range(0, GetTotalEmployees())pelo menos quando comparado a fazer algo mais parecido com isto: (0).To(TotalEmployees) .Where(a => a % 2 == 0) .ToList()// Output:// 0, 2, …

Continuar lendo

Validações funcionais C #

Quando estamos validando nosso modelo, tendemos a escrever validações como esta: public bool IsValid(Product product){if(string.IsNullOrEmpty(product.Name)) return false;if(product.Price<=0) return false;if(product.Category==null) return false; } Isso pode parecer bom no início, mas se …

Continuar lendo

Analisando Xml em C #

Suponha que desejamos extrair alguns objetos de um arquivo xml usando C #. o arquivo xml: <Annotation text=”Brazilian oil giant Petrobras and U.S. oilfield service company Halliburton have signed a …

Continuar lendo