排除查询结果中的一列

excluding a column in query results

我在数据库中有一个名为 EmailLog 的实体,其中包含“收件人”、“发件人”、“主题”、“正文”等条目。

我想在查询多行时排除 efficiency/performance 的正文。不太确定如何做到这一点。我尝试使用投影(见下文),但出现运行时错误,提示我无法投影到实体类型上。

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
            });

这是可以理解的。我可以创建一个没有 Body 的新 class 并投射到它上面,但是我正在对查询应用一些分页并且不想在应用分页选项之前评估结果。我的分页函数如下所示:

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

它接收一个可枚举的列表,并将 Skip() 和 Take() 应用于该列表,然后对其进行枚举。那么我如何确保我将一个 IEnumerable 列表传递给我的函数,它还没有被枚举,但是关联的查询在最终枚举或评估时没有 select 主体?

如我的评论所述,调用 Select() 不会枚举您的结果,它是延迟执行的 Linq 方法之一。因此,如果您对没有 Body 的 class 进行投影,您仍然可以将结果 IEnumerable 传递给您的 ApplyPagingAndSorting() 方法。