Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks David. I went with your approach, but modified it slightly to make it a little less painful form me. I added associations to my classes as in your example:</p> <pre><code>[Association(ThisKey = "EyeColorCode", OtherKey = "Id")] public Code EyeColor { get; set; } [Association(ThisKey = "HairColorCode", OtherKey = "Id")] public Code HairColor { get; set; } </code></pre> <p>Then I wrote an extension method. It takes a new object, and merges all the writable properties into the source object. Using this, I don't need to specify every single property in my query, i.e.:</p> <pre><code>from p in db.Person select p.PropertyUnion(new Person() { { EyeColor = p.EyeColor, HairColor = p.HairColor }; </code></pre> <p>This saves me quite a bit of code for some of my more complex objects, and I think it is more readable. Here is the code for the extension method:</p> <pre><code>/// &lt;summary&gt; /// Union by retrieving all non-null properties from source parameter and setting those properties on the instance object /// &lt;/summary&gt; public static T PropertyUnion&lt;T&gt;(this T destination, T source) { // Don't copy from a null object if (Object.ReferenceEquals(source, null) || Object.ReferenceEquals(destination, null)) { return destination; } // copy properties foreach (var property in source.GetType().GetProperties()) { if (!property.CanWrite || !property.CanRead) continue; var match = destination.GetType().GetProperty(property.Name); if (!match.CanWrite || !match.CanRead) throw new MethodAccessException("Matching property '" + match.Name + "' must allow both read and write operations."); var value = property.GetValue(source, null); if (value != null &amp;&amp; !value.Equals(Activator.CreateInstance(property.PropertyType))) match.SetValue(destination, value, null); } return destination; } </code></pre> <p>Thanks again for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload