Suppose there is a class

public class Information { public string Name; public string Surname; public int Age; } 

There is another class:

 public class InfoByCities { public List<InFormation> Moscow; public List<InFormation> Saint_Petersburg; public List<InFormation> Rostov; } 

Fill in this List<> :

 InfoByCities fill = new InfoByCities(); int Amount = 1500; fill.Moscow = new List<Information>(Amount); for (int i = 0; i < Amount; i++) { Information inf = new Information(); inf.Name = "Саша"+ i.ToString(); inf.Surname = "Поляков" + i.ToString(); inf.Age = i; fill.Moscow.Add(inf); } fill.Saint_Petersburg = new List<Information>(Amount); for (int i = 0; i < Amount; i++) { Information inf = new Information(); inf.Name = "Пётр" + i.ToString(); inf.Surname = "Поляков" + i.ToString(); inf.Age = i; fill.Saint_Petersburg.Add(inf); } fill.Rostov = new List<Information>(Amount); for (int i = 0; i < Amount; i++) { Information inf = new Information(); inf.Name = "Саша" + i.ToString(); inf.Surname = "Петухов" + i.ToString(); inf.Age = i; fill.Rostov.Add(inf); } 

Question: How can I implement a search in all List<> without referring to each individually. For example, find on all three lists of people with the name "Sasha". The number of List<> can be much larger.

  • You have three (and say there may be more) List entities. How do you even see the search in the collection without referring to it directly? And you have all three entities have the same type, therefore, you can search in any of the lists equally. In my opinion, it may look like this: a) We make a list of lists (that is, a container where the variables Moscow , Saint_Petersburg , Rostov and others will be stored). And now we do the search already there, sorting through the contents of each variable. Either b) - you can make 1 list for the search List<InFormation> Search , where we add the contents of all sheets and search. - BlackWitcher
  • But in general, I would have done wrong. A list of type Information would be one, but in the Information class itself add a City field, for example. And you have a typo in the code - compare: InFormation and Information . - BlackWitcher
  • Just the same option I have right now in the program and is used. But very slow. At the expense of a typo: I just gave an example at the expense of cities, names, and so on. Wrote the code here, so I could make a mistake. All the List <> have same type. - Berianidze Luka
  • If the whole thing is in search speed, then you should think about a different approach. For example, instead of List (search O (n)) use HashSet (search O (1)), or SortedList (search O (log n)). See also sentinel value . Well, you can try parallelizing the search. - Alexander Petrov
  • Thank you. But I found another way that is more suitable and does not concern the search in List <>. - Berianidze Luka

1 answer 1

Suppose we have a variable fill type InfoByCities . First you need to combine lists:

 var informations = new List<List<Information>> { fill.Moscow, fill.Saint_Petersburg, fill.Rostov }; var informationArray = informations.SelectMany(i => i).ToArray(); 

Next we do LINQ queries:

 var sashas = informationArray.Where(i => i.Name == "Саша").ToArray();