实体框架linq包括和分组

时间:2012-10-17 12:24:36

标签: c# linq entity-framework

我试图用一句话来包括和分组

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; }
}

但结果是相同的

谢谢

三木

3 个答案:

答案 0 :(得分:2)

您还需要将include放在最后。

喜欢这个......

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

答案 1 :(得分:1)

您尚未在Services课程中为Track定义任何导航属性,您需要添加以下属性。

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

答案 2 :(得分:0)

您的Track类没有名为service的成员访问者,所以

Include("Services")

无效。

您需要链接到Track from Track,例如

public Service Services {get;set;}