2011-10-30 89 views
0

我有一個標籤對象:演員EF查詢結果與來自EF查詢中獲取的擴展屬性擴展類型

public class Tag 
{ 
    public int TagID { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Job> Jobs { get; set; } 

    public Tag() 
    { 
     Jobs = new HashSet<Job>(); 
    } 
} 

和擴展:

public class RecentTag : Tag 
{ 
    public int Count { get; set; } 
} 

...我試圖從查詢添加到每個對象檢索一個RecentTag對象列表:

public IEnumerable<RecentTag> GetRecentTags(int numberofdays) 
{ 
    var tags = Jobs 
     .Where(j => j.DatePosted > DateTime.Now.AddDays(-(numberofdays))) 
     .SelectMany(j => j.Tags) 
     .GroupBy(t => t, (k, g) => new 
     { 
      RecentTag = k, 
      Count = g.Count() 
     }) 
     .OrderByDescending(g => g.Count); 

    // return RecentTags { TagID, Name, Count, Jobs } 
} 

那麼,如何將查詢結果到RecentTag類型並返回擴展對象列表?

任何幫助,將不勝感激。提前致謝!

回答

0

我落得這樣做:

public IEnumerable<RecentTag> GetRecentTags(int numberofdays) 
{ 
    DateTime startdate = DateTime.Now.AddDays(-(numberofdays)); 

    IEnumerable<RecentTag> tags = Jobs 
     .Where(j => j.DatePosted > startdate) // Can't use DateTime.Now.AddDays in Entity query apparently 
     .SelectMany(j => j.Tags) 
     .GroupBy(t => t, (k, g) => new RecentTag 
     { 
      TagID = k.TagID, 
      Name = k.Name, 
      Count = g.Count() 
     }) 
     .OrderByDescending(g => g.Count) 
     .Select(a => a); 

    return tags; 
} 
0

如果Jobs實際上是標籤的集合,並且它們實際上是RecentTag對象,那麼您可以簡單地使用Cast方法。

var rtags = tags.Cast<RecentTags>; 

然而,如果喬布斯不是標籤的集合,那麼你需要投射到一個RecentTags對象..

var rtags = tags.Select(x => new RecentTags() { // assign the members }); 
+0

謝謝。工作不是標籤的集合。但這是我查詢的入口點。 – LukeP