Union of Two Objects Based on Equality of Their Members
<p>Let us start with a class definition for the sake of example:</p>

<pre><code>public class Person
{
    public string FirstName;
    public string LastName;
    public int Age;
    public int Grade;
}
</code></pre>

<p>Now let's assume I have a <code>List&lt;Person&gt;</code> called <code>people</code> containing 3 objects:</p>

<pre><code>{"Robby", "Goki", 12, 8}
{"Bobby", "Goki", 10, 8}
{"Sobby", "Goki", 10, 8}
</code></pre>

<p>What I am looking for is some way to retrieve the following single <code>Person</code> object:</p>

<pre><code>{null, "Goki", -1, 8}
</code></pre>

<p>where fields which are the same in all objects retain their value while fields which have multiple values are replaced with some <strong>invalid</strong> value.</p>

<p>My first thought consisted of:</p>

<pre><code>Person unionMan = new Person();
if (people.Select(p =&gt; p.FirstName).Distinct().Count() == 1)
    unionMan.FirstName = people[0].FirstName;
if (people.Select(p =&gt; p.LastName).Distinct().Count() == 1)
    unionMan.LastName = people[0].LastName;
if (people.Select(p =&gt; p.Age).Distinct().Count() == 1)
    unionMan.Age = people[0].Age;
if (people.Select(p =&gt; p.Grade).Distinct().Count() == 1)
    unionMan.Grade = people[0].Grade;
</code></pre>

<p>Unfortunately, the real business object has <strong>many</strong> more members than four and this is both tedious to write and overwhelming for someone else to see for the first time.</p>

<p>I also considered somehow making use of reflection to put these repetitive checks and assignments in a loop:</p>

<pre><code>string[] members = new string[] { "FirstName", "LastName", "Age", "Grade" };
foreach (string member in members)
{
    if (people.Select(p =&gt; p.**member**).Distinct().Count() == 1)
        unionMan.**member** = people[0].**member**;
}
</code></pre>

<p>where <strong>**<em>member</em>**</strong> would be however reflection would allow the retrieval and storage of that particular member (assuming it is possible).</p>

<p>While the first solution would work, and the second I am assuming would work, does anyone have a better alternative solution to this problem?  If not, would using reflection as described above be feasible?</p>
