I would move this check to a separate method
public static bool ContainsDigit(int input, int digit)
{
do
{
if (input % 10 == digit)
{
return true;
}
input /= 10;
}
while (input > 0);
return false;
}
using:
List<int> result = Allnumber.Where(x => ContainsDigit(x, 3)).ToList();
https://dotnetfiddle.net/2ZPNbM
Same approach on one line
List<int> Allnumber = Enumerable.Range(0, 100).ToList();
List<int> result = Allnumber.Where(x => { do { if (x % 10 == 3) return true; } while ((x /= 10) > 0); return false; }).ToList();
Run (Allnumber with numbers 1000000):
|------------------------------------------------------|
| User | Method | Time |
|------------|--------------------------+--------------|
| fubo | ContainsDigit() | 0,03 seconds |
| JamieC | ToString().Contains("3") | 0,20 seconds |
| TheGeneral | WhereDigit() | 0,10 seconds |
| TheGeneral | InternalRun() | 0,04 seconds |
|------------------------------------------------------|
dotnetfiddle benachmarking - , , - dotnetfiddle, 100,000 1,000,000 , ... https://dotnetfiddle.net/pqCx2J