Dmitriy Sosunov
Dmitriy Sosunov

Reputation: 1113

How to get entity while filtering out certain columns

I have a table with huge columns. How I can select entities without filling properties that are mapped to those columns?

I have one idea, but I'm not sure if it's the right approach:

ctx.Items.Where(....).Select(a=> new Item { ... bind only needed columns }).ToList()

Will those entities be attached to the DataContext?

Upvotes: 1

Views: 97

Answers (2)

Youp Bernoulli
Youp Bernoulli

Reputation: 5655

You are going the right way I think. That's the way for instantiating objects with only the preffered columns / properties filled. Have a look at this post.

EDIT:

This is from Julie Lerman and is what is asked in this question I believe:

Projections with LINQ Query Methods To project using LINQ’s method-based query syntax, you would use the Select method and then identify the properties you want in its parameter. The method-based query syntax requires the syntax for creating an anonymous type in the lambda (see Example 4-4).

Example 4-4. Projecting using LINQ’s method-based syntax

context.Contacts .Where(c => c.FirstName == "Robert") .Select(c => new {c.Title, c.LastName, c.FirstName})

Upvotes: 2

Craig Stuntz
Craig Stuntz

Reputation: 126587

Use any type other than Item. E.g., an anonymous type or DTO. Then your query will do exactly what you want.

Upvotes: 2

Related Questions