Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
Linq join two string list using contains

I am having two lists as follows:

List<string> l1 = new List<string>( new string[]{
  "United States",
  "Brazil",
  "India",
  "Canada"
});

List<string> l2 = new List<string>( new string[]{
 "Asia:India",
 " North America:Canada",
 "EU:Germany"
}) ;

how to list all items from l2 in which contains the string in l1 using Linq Lambda?

Desired Output

"Asia:India"
"North America:Canada"
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

For each item in the second list, check if there is any item in the first list that is part of the item:

l2.Where(i => l1.Any(j => i.Contains(j));

Note that this can be slow for large lists.

over 4 years ago · Santiago Trujillo Relatório

0

If you have a lot of countries to look for, I suggest using HashSet<string> (it has better Contains time compexity, O(1) vs O(N)):

  HashSet<string> countries = new HashSet<string>() {
    "United States",
    "Brazil",
    "India",
    "Canada",
  };

To create countries from l1:

  HashSet<string> countries = new HashSet<string>(l1);

Then you can put a simple Linq:

  var result = l2
    .Where(item => countries.Contains(item.Substring(item.IndexOf(':') + 1))); 

You data has inner structure: Area:Country which we can exploit with a help of Substring and IndexOf

Demo:

  var l1 = new List<string>() { 
    "United States", 
    "Brazil", 
    "India", 
    "Canada", 
    "Congo",  // <- My special test case 
  };
  
  var l2 = new List<string>() { 
    "Asia:India", 
    "North America:Canada", 
    "EU:Germany", 
    "Africa:Democratic Republic of Congo" 
  };

  HashSet<string> countries = new HashSet<string>(l1);

  var result = l2
    .Where(item => countries.Contains(item.Substring(item.IndexOf(':') + 1)));

  Console.Write(string.Join("; ", result));

Outcome:

  Asia:India; North America:Canada

Note abscence of unwanted Africa:Democratic Republic of Congo: Congo and Democratic Republic of Congo are different states.

Edit: Linq Join solution

 var result = l2.Join(
   l1, 
   item => item.Substring(item.IndexOf(':') + 1), 
   item => item, 
  (left, right) => left);
over 4 years ago · Santiago Trujillo Relatório

0

var answer = l2.Where(x => l1.Any(l => x.Contains(l)));

if the l1 is a large list you can use a HashSet instead of l1 to improve the time.

Alternative way you can split each element of l2 by :

var answer = l2.Where(x => l1.Contains(x.Split(':')[1]));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda