2012-10-17 226 views
3

我試圖做的包括和組一句話實體框架LINQ包括和分組

var instanceIdList = context. 
    Tracks. 
    Include("Services"). 
    GroupBy(x => x.ServiceId). 
    Take(top); 

,但是當我在調試檢查結果我看不到任何有值的

我試圖用另一種方式

var objectContext = ((IObjectContextAdapter)context).ObjectContext; 
var set = objectContext.CreateObjectSet<Track>(); 
var instanceIdList = set.Include("Services").GroupBy(x => x.ServiceId); 

這做的是類: 軌道

public partial class Track 
{ 
    public long Id { get; set; } 
    public System.Guid ServiceId { get; set; } 
    public Nullable<System.Guid> ServiceInterfaceId { get; set; } 
    public Nullable<System.Guid> ProviderId { get; set; } 
    public System.Guid ServiceInstanceId { get; set; } 
    public System.Guid ActivityParentId { get; set; } 
    public System.Guid ActivityInstanceId { get; set; } 
    public int ActivityType { get; set; } 
    public int ServiceRole { get; set; } 
    public int TrackOrder { get; set; } 
    public System.DateTime Datetime { get; set; } 
    public Nullable<System.Guid> MessageId { get; set; } 
    public int Status { get; set; } 
    public Nullable<int> ESBErrorCode { get; set; } 
    public Nullable<int> ESBTecnicalErrorCode { get; set; } 
    public string ErrorDescription { get; set; } 
    public string PortName { get; set; } 
    public string MachineName { get; set; } 
    public string ConsumerId { get; set; } 
    public string ExternalId { get; set; } 
    public string ConsumerMachineName { get; set; } 
    public int ServiceBehavior { get; set; } 

    public virtual Message Message { get; set; } 
} 

服務

 public partial class Service 
     { 
     public Service() 
     { 
     this.Providers = new HashSet<Provider>(); 
     this.ServiceInterfaces = new HashSet<ServiceInterface>(); 
      } 

    public System.Guid ServiceId { get; set; } 
    public string ServiceName { get; set; } 
    public string ServiceNumber { get; set; } 
    public Nullable<System.Guid> ModelSchemaId { get; set; } 

    public virtual ICollection<Provider> Providers { get; set; } 
    public virtual ICollection<ServiceInterface> ServiceInterfaces { get; set; } 
} 

,但結果是一樣的

感謝

三木

+0

是什麼曲目和服務之間的關係? – JeffreyABecker

+0

@JeffreyABecker我加了班,沒有關係 – MIkCode

回答

2

你還需要把include包括在最後。

就像這個...

var instanceIdList = context. 
    Tracks. 
    GroupBy(x => x.ServiceId). 
    Take(top). 
    Include("Services"); 
0

航跡類沒有調用的服務的成員存取,所以

Include("Services") 

不起作用。

您需要鏈接到跟蹤服務,例如,

public Service Services {get;set;} 
1

你有沒有在你的Track類中定義爲Services任何導航屬性,則需要添加以下屬性。

public virtual ICollection<Service> Services { get; set; }