2012-08-27 105 views
3

我不明白爲什麼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; } 
     } 
+0

是的,這不應該發生(對於這兩個查詢)。 – usr

+0

你的'EmailDetails'是什麼類型? –

+0

嗨拉迪斯拉夫,我已經更新了問題,並加入了我加入的兩張桌子的基礎知識(留出專欄,我不認爲這件事會讓帖子稍微短一點) –

回答

2

我沒有得到相同的結果你。使用下面的控制檯應用程序:

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; } 
     public bool WhiteList { get; set; } 
    } 

    public class User 
    { 
     public long UserId { get; set; } 
     public string Username { get; set; } 
    } 

    public class CFContext : DbContext 
    { 
     public DbSet<AddressBookEntry> AddressBookEntries { get; set; } 
     public DbSet<User> Users { get; set; } 
     public DbSet<EmailDetail> EmailDetails { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var db = new CFContext(); 

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

      string username = "1"; 

      var x = db.EmailDetails.Where(a => a.User.Username == username && a.FromAddressBookEntry.WhiteList). 
             Select(a => new 
             { 
              a.EmailSendFinishTime 
             }). 
             OrderByDescending(
              a => a.EmailSendFinishTime) 
             .FirstOrDefault(); 

      Console.WriteLine(y); 
      Console.WriteLine(x); 
     } 
    } 

我這些SQL查詢

SELECT TOP (1) 
[Extent1].[EmailSendFinishTime] AS [EmailSendFinishTime] 
FROM [dbo].[EmailDetails] AS [Extent1] 
WHERE [Extent1].[FromAddressBookEntryId] IS NOT NULL 
ORDER BY [Extent1].[EmailSendFinishTime] DESC 


SELECT TOP (1) 
[Project1].[C1] AS [C1], 
[Project1].[EmailSendFinishTime] AS [EmailSendFinishTime] 
FROM (SELECT 
    [Extent1].[EmailSendFinishTime] AS [EmailSendFinishTime], 
    1 AS [C1] 
    FROM [dbo].[EmailDetails] AS [Extent1] 
    INNER JOIN [dbo].[Users] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[UserId] 
    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 

還有什麼在你的模型,也許還有別的使上面的SQL查詢?

+0

嗨,馬克,你正在得到我會有預期的。讓我在我的項目中嘗試一下你的例子,看看會發生什麼,並返回結果。感謝您的努力! –

+0

對我來說壞消息。我試過你的代碼,確認它使用了相同版本的EF(v4.0.30319),並且你發佈的代碼產生了你提到的結果,但是我的代碼(有相同的列,只是一些被刪除)返回所有額外的列。看起來我有一個「差異」追捕我。當我縮小到希望導致問題的原因時,我會回覆。至少它正在返回正確的答案,我可以告訴,因爲c#接近sqlserver它對我來說不是一個大問題。再次感謝。 -Peter –

相關問題