title: Expression to create a Tuple with variable number of generic type arguments
body: <p>I'm trying to build an expression for creating a generic <code>Tuple&lt;&gt;</code> instance with a variable number of generic type arguments.</p>
  
  <p>The idea for the generated <code>Tuple&lt;&gt;</code> instance is to dynamically create a composite key value for entity types based on properties that have a <code>KeyAttribute</code> on it. The composite key would then be used as a key in a <code>Dictionary&lt;object, TEntity&gt;</code>. So the lambda expression should be built for a certain entity type and the lambda would be called, passing an instance of <code>TEntity</code> to get back a composite key in the form of a <code>Tuple&lt;&gt;</code>.</p>
  
  <p><strong>Example entity model</strong></p>
  
  <pre><code>public class MyEntityModel
  {
      [Key]
      public string Key1 { get; set; }
      [Key]
      public Guid Key2 { get; set; }
      public int OtherProperty { get; set; }
  }
  </code></pre>
  
  <p><strong>What expression should do</strong></p>
  
  <pre><code>public Func&lt;MyEntityModel, object&gt; BuildKeyFactory()
  {
      // This is how the LambdaExpression should look like, but then for a generic entity type instead of fixed to MyEntityModel
      return new Func&lt;MyEntityModel, object&gt;(entity =&gt; new Tuple&lt;string, Guid&gt;(entity.Key1, entity.Key2));
  }
  </code></pre>
  
  <p>But of course the entity model needs to be a generic type.</p>
  
  <p><strong>What I have so far</strong></p>
  
  <pre><code>public Func&lt;TEntity, object&gt; BuildKeyFactory&lt;TEntity&gt;()
  {
      var entityType = typeof(TEntity);
  
      // Get properties that have the [Key] attribute
      var keyProperties = entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
          .Where(x =&gt; x.GetCustomAttribute(typeof(KeyAttribute)) != null)
          .ToArray();
  
      var tupleType = Type.GetType($"System.Tuple`{keyProperties.Length}");
      if (tupleType == null) throw new InvalidOperationException($"No tuple type found for {keyProperties.Length} generic arguments");
  
      var keyPropertyTypes = keyProperties.Select(x =&gt; x.PropertyType).ToArray();
      var tupleConstructor = tupleType.MakeGenericType(keyPropertyTypes).GetConstructor(keyPropertyTypes);
      if (tupleConstructor == null) throw new InvalidOperationException($"No tuple constructor found for key in {entityType.Name} entity");
  
      // The following part is where I need some help with...
      var newTupleExpression = Expression.New(tupleConstructor, keyProperties.Select(x =&gt; ????));
  
      return Expression.Lambda&lt;Func&lt;TEntity, object&gt;&gt;(????).Compile();
  }
  </code></pre>
  
  <p>As you can see, I'm not able to figure out how I need to create property expressions for passing to the <code>Expression.New()</code> call (probably something with <code>Expression.MakeMemberAccess(Expression.Property())</code> but don't know how to pass the <code>TEntity</code> instance from the lambda argument) and how I can 'chain' this with the <code>Expression.Lambda</code> call. Any help would be highly appreciated!</p>