Answers for "c# search a value in a list"

C#
1

Search for a value into a list in c#

//In this example i have 2 lists with the months numbers and i will check 
//if the month have 31 or 30 days, using the method .IndexOf()
//this method search the array content and return a zero-based value,
//if found, otherwise return -1. In this example its using a List<int>
//the same process can be apllied for every list
List<int> m31 = new List<int>{ 1, 3, 5, 7, 8, 10, 12 };
List<int> m30 = new List<int>{ 4, 6, 9, 11 };

public string ValidaData(int month, int year)
{
  if (m30.IndexOf(mes) != -1)
  {
    return "have 30 days";
  }
  else if(m31.IndexOf(mes) != -1)
  {
  	return "have 30 days";
  }
  else if(mes == 2)
  {
  	if(year % 4 == 0)
      return "have 29 days";
    else
       return "have 28 days";
  }
  else
  {
  	return "Unknown month";
  }
}
Posted by: Guest on February-09-2022
2

c# find item in list

MyClass result = list.Find(x => x.GetId() == "xy");
Posted by: Guest on September-28-2021
0

list search c#

list.Where(x => x.Name == "theName");
return list;
Posted by: Guest on February-09-2021

Code answers related to "c# search a value in a list"

C# Answers by Framework

Browse Popular Code Answers by Language