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

347
Visualizações
Select all from one List, replace values that exist on another List

Few days ago I asked same question with SQL, but now it arises in C# code

Lets say we have this kind of class for holding different id/text pairs:

public class Text {
    public int id { get; set; }
    public string text { get; set; }
    ...
}

Now lets populate some data, ListA gets a lot of data:

List<Text> ListA = new List<Text>{
  new () {id = 1, text = "aaa1"},
  new () {id = 2, text = "aaa2"},
  new () {id = 3, text = "aaa3"},
  new () {id = 4, text = "aaa4"},
  new () {id = 5, text = "aaa5"},
  new () {id = 6, text = "aaa6"},
};

And ListB gets just a little bit of data:

List<Text> ListB = new List<Text>{
  new () {id = 4, text = "bbb4"},
  new () {id = 5, text = "bbb5"},
};

And now what we are looking:

var result = ... // Some Linq or Lambda magic goes here

// and if we do:
foreach(var item in result){
     Console.WriteLine(item.Id + " " + item.Text);
}

// Result will be:
1 : aaa1
2 : aaa2
3 : aaa3
4 : bbb4
5 : bbb5
6 : aaa6
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can try looking for id within ListB:

var result = ListA
  .Select(a => ListB.FirstOrDefault(b => b.id == a.id) ?? a);

Here for each a within ListA we try to find corresponding by id (b.id == a.id) item within ListB. If no such item is found we just return ListA item: ?? item

In case of .Net 6 you can use overloaded .FirstOrDefault version (we can pass a as a default value):

var result = ListA
  .Select(a => ListB.FirstOrDefault(b => a.id == b.id, a));
over 4 years ago · Santiago Trujillo Relatório

0

It might be more efficient to convert ListB to a Dictionary first: var dictB = ListB.ToDictionary(x=> x.id)

Then you can write

var result = ListA.Select(x => dictB.TryGetValue(x.id, out var b) ? b : x)

UPD Rewrote taking comment suggestions into account

over 4 years ago · Santiago Trujillo Relatório

0

One option is to do an Union operation, by specifying an EqualityComparer. If the order is important, you can do an OrderBy operation at the end.

class TextIdComparer : EqualityComparer<Text> {
    public override bool Equals(Text x, Text y) => x.id == y.id;
}

var result = ListB.Union(ListA, new TextIdComparer()).OrderBy(x => x.id)
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