简体   繁体   English

排除查询结果中的列

[英]excluding a column in query results

I have an entity called EmailLog with entries such as To, From, Subject, Body, etc. in the database. 我有一个名为EmailLog的实体,在数据库中具有诸如To,From,Subject,Body等条目。

I would like to exclude Body for efficiency/performance when querying for multiple rows. 查询多个行时,我想排除Body的效率/性能。 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. 我尝试使用投影(请参见下文),但是出现运行时错误,提示我无法投影到Entity类型。

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. 我可以创建一个没有Body的新类,并在其上进行投影,但是我正在对查询应用分页,并且直到应用分页选项后才想评估结果。 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. 它接受一个可枚举的列表,并将Skip()和Take()应用于列表,然后枚举它。 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? 因此,如何确保将IEnumerable列表传递给尚未枚举的函数,但最终枚举或求值时关联的查询未选择Body?

As mentioned in my comment, calling Select() will not enumerate your result, it is one of the Linq methods that defer execution. 如我的评论中所述,调用Select()不会枚举您的结果,这是延迟执行的Linq方法之一。 So if you do a projection to a class without Body, you can still pass the resulting IEnumerable to your ApplyPagingAndSorting() method. 因此,如果对没有Body的类进行投影,则仍然可以将所得IEnumerable传递给ApplyPagingAndSorting()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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