1

I am writing a LINQ query where i want to check if the file name contains one of the following values: ".xls", "xlsx", ".csv"

var results = api.GetAttachments(id).Where(x => 
                  x.fileName.Contains(".xls") ||
                  x.fileName.Contains(".xlsx") || 
                  x.fileName.Contains(".csv"))
              .ToList();

I would like to reduce repetition in this linq query and make the file types a variable. So I wrote a list of string variable

List<string> fileTypes = new List<string>(new string[] {".xls", ".xlsx", ".csv" });

How can i incorporate this list of strings in the first query? Basically I want to select objects if the file name is one of the strings mentioned above.

Thanks in advance.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Fast Chip
  • 425
  • 1
  • 4
  • 16
  • Would you consider calling your linq query inside a foreach loop for `fileTypes` ? – 0014 Sep 26 '17 at 20:29
  • 1. What happens if you send a filename with Cap Extension .XLS? 2. What happens if the file name is `myFile.csv.txt` dont use contains use path get extension and a case insensitive equals – johnny 5 Sep 26 '17 at 20:45

0 Answers0