44

I have two IEnumerable<T>s.

One gets filled with the fallback ellements. This one will always contain the most elements. The other one will get filled depending on some parameters and will possibly contain less elements. If an element doesn't exist in the second one, I need to fill it with the equivalent one of the first one.

This code does the job, but feels inefficient to me and requires me to cast the IEnumerables to ILists or to use a temporary list Person implements IEquatable

IEnumerable<Person> fallBack = Repository.GetPersons();
IList<Person> translated = Repository.GetPersons(language).ToList();

foreach (Person person in fallBack)
{
    if (!translated.Any(p=>p.equals(person)))
        translated.add(person);  
}

Any suggestions?

3 Answers 3

50
translated.Union(fallback)

or (if Person doesn't implement IEquatable<Person> by ID)

translated.Union(fallback, PersonComparer.Instance)

where PersonComparer is:

public class PersonComparer : IEqualityComparer<Person>
{
    public static readonly PersonComparer Instance = new PersonComparer();

    // We don't need any more instances
    private PersonComparer() {}

    public int GetHashCode(Person p)
    {
        return p.id;
    }

    public bool Equals(Person p1, Person p2)
    {
        if (Object.ReferenceEquals(p1, p2))
        {
            return true;
        }
        if (Object.ReferenceEquals(p1, null) ||
            Object.ReferenceEquals(p2, null))
        {
            return false;
        }
        return p1.id == p2.id;
    }
}
5
  • That will only work though if the Person class properly implements Equality. Judging by the OP I'm guesssing they don't.
    – JaredPar
    Feb 26, 2009 at 15:25
  • HAHA! Jon, couldn't you package it in more mumbo jumbo? I feel utterly stupid again :P Feb 26, 2009 at 15:26
  • Actually they do. The OP also contains no real code. Partly because it simplifies my question and partly because my boss is terified of putting code in public areas :s Feb 26, 2009 at 15:27
  • @Jared: I was getting there :)
    – Jon Skeet
    Feb 26, 2009 at 15:28
  • 2
    Actually, I fear it seems it's not the solution I was aiming for. This will give me the collection with ALL elements from both in it and it will never hit my Equals(Person other) method. Am I doing something wrong or did I miscommunicate my question? Feb 26, 2009 at 15:45
34

Try this.

public static IEnumerable<Person> SmartCombine(IEnumerable<Person> fallback, IEnumerable<Person> translated) {
  return translated.Concat(fallback.Where(p => !translated.Any(x => x.id.equals(p.id)));
}
2
  • 4
    This works great but there is a gotcha that you must be aware of: programmaticallyspeaking.com/…
    – Oliver
    Mar 16, 2015 at 14:09
  • I'd avoid consuming IEnumerable multiple times. As "under the hood" it can reconnect to DB or do some calculations on each enumeration. Or, say, just be infinite... Apr 20, 2019 at 12:56
0

use Concat. Union does not work in case List<dynamic> type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.