简体   繁体   中英

excluding a column in query results

I have an entity called EmailLog with entries such as To, From, Subject, Body, etc. in the database.

I would like to exclude Body for efficiency/performance when querying for multiple rows. Not really sure how to accomplish that. I tried using projection (see below), but I get a runtime error saying I can't project onto the Entity type.

var results = Repository.Find<EmailLog>().Select(x => new EmailLog
            {
                Id = x.Id,
                Subject = x.Subject,
                Recipient = x.Recipient,
                Status = x.Status,
                FirstAttempted = x.FirstAttempted,
                LastAttempted = x.LastAttempted,
                Attempts = x.Attempts
            });

This is understandable. I can create a new class without Body and project onto that, but I am applying some pagination to the query and don't want to evaluate the results till after I've applied pagination options. My pagination function looks like this:

public static PagedList<T> ApplyPagingAndSorting<T>(this IEnumerable<T> list, 
IPaginationOptions paginationOptions)

It takes in an enumerable list and applies Skip() and Take() to the list and then enumerates it. So how can I make sure I pass in an IEnumerable list to my function which hasn't been enumerated yet, but the associated query doesn't select Body when finally enumerated or evaluated?

As mentioned in my comment, calling Select() will not enumerate your result, it is one of the Linq methods that defer execution. So if you do a projection to a class without Body, you can still pass the resulting IEnumerable to your ApplyPagingAndSorting() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM