John W
John W

Reputation: 3

Using EF and Linq and exclude properties in a select statement

The methods are created via Entity Framework and Linq in VS2012 and C#, as such, I want to call a model named Categories. Each category, has a parentID (either null or set), which encounts for multiplicity. Thus, we have 1 parent Category, and that category has Children of categories, a

System.Data.Objects.DataClasses.EntityCollection<< Category >>

However, each category has a single property which contains a massive load of data, we only want to load that specific property on the parent. Current code is:

selectedCategory = categoryObjectContext.Category.Single<Category>(cat => cat.CategoryID == selectedID);

Upvotes: 0

Views: 1364

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78595

You need to project the results from your query using .Select() and use a Where() clause to filter the data:

int parentID = categoryObjectContext.Category.Where(cat => cat.CategoryID == selectedID)
                   .Select(cat => cat.parentID) // Project the query results into a single field
                   .First(); // This will only select one column

Upvotes: 1

Related Questions