我不明白爲什麼entityframework返回給我以下兩個查詢的所有列。我試圖讓它回到我剛纔的日期列,但回db.EmailDetails的所有列來時,我看着生成的SQL(貼在下面)EntityFramework 4返回所有列,當我只想要一個返回
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; }
}
是的,這不應該發生(對於這兩個查詢)。 – usr
你的'EmailDetails'是什麼類型? –
嗨拉迪斯拉夫,我已經更新了問題,並加入了我加入的兩張桌子的基礎知識(留出專欄,我不認爲這件事會讓帖子稍微短一點) –