Linq向实体导航属性的复杂性

时间:2013-12-03 19:27:37

标签: linq-to-entities

我有3个实体,中间的一个加入外部的两个。 我有一个RequestType1的值,我需要的是使用Linq的RequestField1的相应值。 RequestType1的值为2。

我的课程看起来像这样

 public partial class RequestField
{
    public int RequestID { get; set; }
    public int RequestField1 { get; set; }
    public string FieldContents { get; set; }

    public virtual FieldDefinition FieldDefinition { get; set; }
    public virtual Request Request { get; set; }
}

public partial class FieldDefinition
{
    public FieldDefinition()
    {
        this.RequestFields = new HashSet<RequestField>();
        this.RequestTypes = new HashSet<RequestType>();
    }

    public int RequestField { get; set; }
    public string FieldName { get; set; }
    public string FieldReference { get; set; }

    public virtual ICollection<RequestField> RequestFields { get; set; }
    public virtual ICollection<RequestType> RequestTypes { get; set; }
}

public partial class RequestType
{
    public RequestType()
    {
        this.Requests = new HashSet<Request>();
        this.FieldDefinitions = new HashSet<FieldDefinition>();
    }

    public int RequestType1 { get; set; }
    public string TypeDescription { get; set; }

    public virtual ICollection<Request> Requests { get; set; }
    public virtual ICollection<FieldDefinition> FieldDefinitions { get; set; }
}

1 个答案:

答案 0 :(得分:0)

如果我正确地阅读你的代码,那就是这样的:

var requestFields = myRequestType.SelectMany(
                                   rt => rt.FieldDefinitions.SelectMany(
                                          fd => fd.RequestFields.Select(
                                               rf => rf.RequestField1)));