Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is this what you are after?</p> <pre><code>class Foo { public string Property1 { get; set; } public string Property2 { get; set; } } Func&lt;Foo, object&gt; lambda = foo =&gt; new { foo.Property1, foo.Property 2 }; var foo = new Foo { Property1 = "foo", Property2 = "bar" }; var anon = lambda(foo); </code></pre> <p>If so, and especially since you are talking about entities, I would suggest that you:</p> <ol> <li>Use <a href="http://msdn.microsoft.com/en-us/library/system.tuple.aspx" rel="nofollow"><code>Tuple</code></a> instead of anonymous types. Tuples are also immutable, which is a plus when you want to use them as dictionary keys.</li> <li>Integrate the unique key creation function in your entity class instead of spinning it out into an external function (anonymous or not). It's your entity that should know which of its properties make up a unique key, not some external code.</li> </ol> <p>For example:</p> <pre><code>class Foo { public string Property1 { get; set; } public string Property2 { get; set; } public object GetUniqueKey() { return Tuple.Create(this.Property1, this.Property2); } } </code></pre> <p>There's still an issue with the code above: <code>Property1</code> and <code>Property2</code> are publicly settable, which means that after you put <code>Foo</code> instances in a map their keys might not match their values if e.g. <code>Property1</code> is later modified. However this is a problem that occurs quite often in practice and there's really no good solution that I know of other than "just don't mess with the object properties then".</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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