我不明白为什么 entityframework 会返回以下两个查询中的所有列。我试图让它只返回日期列,但返回 db.EmailDetails 的所有列,当我查看生成的 sql 时(发布在下面)
var y = (from data1 in db.EmailDetails
join data2 in db.AddressBookEntries on data1.FromAddressBookEntryId equals data2.Id
orderby data1.EmailSendFinishTime descending
select data1.EmailSendFinishTime).FirstOrDefault();
var x =
db.EmailDetails.Where(a => a.User.Username == username && a.FromAddressBookEntry.WhiteList).
Select(a => new
{
a.EmailSendFinishTime
}).
OrderByDescending(
a => a.EmailSendFinishTime)
.FirstOrDefault();
生成的sql:
exec sp_executesql N'SELECT TOP (1)
[Project1].[Id] AS [Id],
[Project1].[UserId] AS [UserId],
[Project1].[InValidMailMessage] AS [InValidMailMessage],
[Project1].[MailInternalDate] AS [MailInternalDate],
[Project1].[MessageUniqueId] AS [MessageUniqueId],
[Project1].[FromAddressBookEntryId] AS [FromAddressBookEntryId],
[Project1].[ToEmailAddress] AS [ToEmailAddress],
[Project1].[EmailFolderId] AS [EmailFolderId],
[Project1].[EmailHeaderInfo] AS [EmailHeaderInfo],
[Project1].[EmailSendSmtpServer] AS [EmailSendSmtpServer],
[Project1].[EmailSendStatus] AS [EmailSendStatus],
[Project1].[EmailSendStartTime] AS [EmailSendStartTime],
[Project1].[EmailSendFinishTime] AS [EmailSendFinishTime],
[Project1].[EmailSendLogMessage] AS [EmailSendLogMessage],
[Project1].[Subject] AS [Subject],
[Project1].[MimeMessageFull] AS [MimeMessageFull],
[Project1].[HighPriority] AS [HighPriority],
[Project1].[SentDateTime] AS [SentDateTime],
[Project1].[EmailDeleted] AS [EmailDeleted],
[Project1].[EmailDeletedDateTime] AS [EmailDeletedDateTime],
[Project1].[EmailViewed] AS [EmailViewed],
[Project1].[EmailViewedDateTime] AS [EmailViewedDateTime],
[Project1].[BodyTextForSend] AS [BodyTextForSend],
[Project1].[ReceivedDate] AS [ReceivedDate]
FROM ( SELECT [Extent1].[Id] AS [Id],
[Extent1].[UserId] AS [UserId],
[Extent1].[InValidMailMessage] AS [InValidMailMessage],
[Extent1].[MailInternalDate] AS [MailInternalDate],
[Extent1].[MessageUniqueId] AS [MessageUniqueId],
[Extent1].[FromAddressBookEntryId] AS [FromAddressBookEntryId],
[Extent1].[ToEmailAddress] AS [ToEmailAddress],
[Extent1].[EmailFolderId] AS [EmailFolderId],
[Extent1].[EmailHeaderInfo] AS [EmailHeaderInfo],
[Extent1].[EmailSendSmtpServer] AS [EmailSendSmtpServer],
[Extent1].[EmailSendStatus] AS [EmailSendStatus],
[Extent1].[EmailSendStartTime] AS [EmailSendStartTime],
[Extent1].[EmailSendFinishTime] AS [EmailSendFinishTime],
[Extent1].[EmailSendLogMessage] AS [EmailSendLogMessage],
[Extent1].[Subject] AS [Subject],
[Extent1].[MimeMessageFull] AS [MimeMessageFull],
[Extent1].[HighPriority] AS [HighPriority],
[Extent1].[SentDateTime] AS [SentDateTime],
[Extent1].[EmailDeleted] AS [EmailDeleted],
[Extent1].[EmailDeletedDateTime] AS [EmailDeletedDateTime],
[Extent1].[EmailViewed] AS [EmailViewed],
[Extent1].[EmailViewedDateTime] AS [EmailViewedDateTime],
[Extent1].[BodyTextForSend] AS [BodyTextForSend],
[Extent1].[ReceivedDate] AS [ReceivedDate]
FROM [dbo].[EmailDetails] AS [Extent1]
INNER JOIN [dbo].[Users] AS [Extent2] ON [Extent1].[UserId] =
[Extent2].[Id]
INNER JOIN [dbo].[AddressBookEntries] AS [Extent3] ON
[Extent1].[FromAddressBookEntryId] = [Extent3].[Id]
WHERE ([Extent2].[Username] = @p__linq__0) AND
([Extent3].[WhiteList] = 1)) AS [Project1]
ORDER BY [Project1].[EmailSendFinishTime] DESC',N'@p__linq__0 nvarchar(4000)
',@p__linq__0=N'ekellner9
EmailDetail 定义和 AddressBookEntry 定义
public class EmailDetail
{
public EmailDetail()
{
// keep sqlserver from blowing up with no datetime set
EmailSendStartTime = new DateTime(1900, 1, 1);
EmailSendFinishTime = new DateTime(1900, 1, 1);
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[ForeignKey("User")]
public long UserId { get; set; }
[Required(ErrorMessage = "Must have user associted with EmailDetail")]
public User User { get; set; }
public long? FromAddressBookEntryId { get; set; }
public AddressBookEntry FromAddressBookEntry { get; set; }
public DateTime EmailSendStartTime { get; set; }
public DateTime EmailSendFinishTime { get; set; }
}
public class AddressBookEntry
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[ForeignKey("User")]
public long UserId { get; set; }
[Required]
public User User { get; set; }
[MaxLength(512)]
[Required(ErrorMessage = "Email is required")]
public string Email { get; set; }
}