I have a list. In it elements of the form: "_something": "",

So, I would like to know how to search the list so that, having only something without quotes and underscores, find the index of the element. Based on whether there is something inside, I will need to perform different actions.

List.Contains, as I understand, does not fit, List.Where, too, List.Exists also seems to not work, or I could not express the conditions correctly. Thank you all for your help!

Using List fileContent = File.ReadAllLines (filesList [i]). ToList (); I collect all rows from JSON, as in the screenshot, then I pull out a new table from the database, if its name is already present inside the list, I just check if all the keys are there, if not, then add those that are not. If the table name is not, respectively, add to the end of the file.

enter image description here

  • элементы вида: "_something":"" - is it not json that you are trying to read with a crutch? - EvgeniyZ
  • @EvgeniyZ yes, yes, it is his. But I am undergoing an internship and were told to work with JSON, as with a regular text document - Nataila
  • Horror! Well, what type of sheet does the string have? - EvgeniyZ
  • @EvgeniyZ yes. Now I correct the question, so that it is easier to understand. - Nataila
  • Horror! You need to test skills on real tasks. - AK

1 answer 1

Well, let's say there is a collection:

 List<string> list = new List<string> { "_something1\":\"", "_something2\":\"", "_something3\":\"", "_something4\":\"", "_something5\":\"" }; 

We have only something4 . We need to find the element that it contains:

 list.FirstOrDefault(x => x.Contains("something4")); 

If we need to get the item number in the collection, then wrap everything in IndexOf and get something like this:

 var index = list.IndexOf(list.FirstOrDefault(x => x.Contains("something4"))); 
  • @Nataila FirstOrDefault will return NULL if the item is not found. And if it doesn't find IndexOf , the value will be -1 . - EvgeniyZ
  • I understood, thank you :) - Nataila
  • @Nataila FirstOrDefault essentially returns us the same type that lies in the collection (string, int, bool, class, etc.), if the value is not found, the default value of this type will be set. - EvgeniyZ
  • Thanks again :) Now everything is clear. - Nataila
  • @Nataila Yes, not at all. Glad I could help. Well, in general, is it worth your "teachers" in the neck to give for this ... - EvgeniyZ