سؤال

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.

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top