Most efficient way to convert a object to another (Model to ViewModel)
<p>Suppose I have a model with 20 fields, and in my index page, I want to list all models that are stored in my database.
In index page, instead of listing all fields of the model, I only to list 3 fields.</p>

<p>So, I make two class:</p>

<pre><code>class CompleteModel {
    public int Id { get; set; }
    public string Field01 { get; set; }
    public string Field02 { get; set; }
    public string Field03 { get; set; }
    public string Field04 { get; set; }
    public string Field05 { get; set; }
    ...
    public string Field20 { get; set; }
}
</code></pre>

<p>now, in my Controller, I can use:</p>

<pre><code>await _context.CompleteModel.ToListAsync();
</code></pre>

<p>but I feel that it does not seem to be the right way to do it, because I'm getting all fields and using only 3 fields.</p>

<p>So, I made this code:</p>

<pre><code>class ViewModel {
    public string Field02 { get; set; }
    public string Field04 { get; set; }
    public string Field08 { get; set; }
}

var result = _context.CompleteModel.Select(
    x =&gt; new {
        x.Field02,
        x.Field04,
        x.Field08
    }).ToListAsync();

var listResults = new List&lt;IndexViewModel&gt;();
if (result != null)
{
    listResults.AddRange(results.Select(x =&gt; new IndexViewModel
    {
        Field02 = x.Field02,
        Field04 = x.Field04,
        Field08 = x.Field08
    }));
}
</code></pre>

<p>I think this is a lot of code to do this.
First, I selected all the fields that I want, then, copied everything to another object. </p>

<p>There's a "more directly" way to do the same thing?</p>

<p>Like:</p>

<pre><code>_context.CompleteModel.Select(x =&gt; new IndexViewModel { Field02, Field04, Field08 });
</code></pre>