Frage

Is it possible to load only specific attributes of a class, such as only the primary key, without fetching the other attribute values?

I need to get an object from the DB but only have the ID value populated, all other attributes are not needed. If another attribute is requested, the rest of the object needs to be loaded.

War es hilfreich?

Lösung

Yes you can and technique is called "Projection"
e.g.

var filteredData = context.Entity
.Where(p => p.ID == 1 ) // just an example filter
.Select(p => new Entity
{
    ID = p.ID,        
})
.ToList();

If if helps dont forgot to vote and mark it as answer :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top