Question

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.

Was it helpful?

Solution

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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top