Do you only project properties you need?

Loading last updated info...

When retrieving data it's much more efficient to only collect the data you need. It saves computation and IO on the database and also saves memory and CPU on the calling side.

❌ Figure: ```cs IEnumerable<string> GetProductGuids(string category) { IEnumerable<Product> products = context.Products .Where(x => x.Category == category) .ToList(); return products.Select(x => x.ProductGuid); } ``` Figure: Bad example - Retrieved the whole product record when we only needed 1 property

✅ Figure: ```cs IEnumerable<string> GetProductGuids(string category) { IEnumerable<string> productGuids = context.Products .Where(x => x.Category == category) .Select(x => x.ProductGuid) .ToList(); return productGuids; } ``` Figure: Good example - Retrieved only the required property.

Authors

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star