Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问ICollection类型的导航属性_C#_Linq_Entity Framework_Lambda - Fatal编程技术网

C# 访问ICollection类型的导航属性

C# 访问ICollection类型的导航属性,c#,linq,entity-framework,lambda,C#,Linq,Entity Framework,Lambda,我有两个实体类 namespace REApplications.BLL { using System; using System.Collections.Generic; public partial class Hr { public Hr() { this.HrCompensation = new HashSet<HrCompensation>(); this.HrSplitPlan = new HashSet<HrSpl

我有两个实体类

namespace REApplications.BLL
{
using System;
using System.Collections.Generic;

public partial class Hr
{
    public Hr()
    {
        this.HrCompensation = new HashSet<HrCompensation>();
        this.HrSplitPlan = new HashSet<HrSplitPlan>();
    }

    public const string Schema_Id = "Id";
    public int Id { get; set; }
    public const string Schema_ContactId = "ContactId";
    public int ContactId { get; set; }
    public const string Schema_BaseOfficeLocation = "BaseOfficeLocation";
    public string BaseOfficeLocation { get; set; }
    public const string Schema_Type = "Type";
    public string Type { get; set; }
    public const string Schema_EmploymentStatus = "EmploymentStatus";
    public string EmploymentStatus { get; set; }
    public const string Schema_EmploymentStartDate = "EmploymentStartDate";
    public Nullable<System.DateTime> EmploymentStartDate { get; set; }
    public const string Schema_EmploymentEndDate = "EmploymentEndDate";
    public Nullable<System.DateTime> EmploymentEndDate { get; set; }
    public const string Schema_CreatedByContactId = "CreatedByContactId";
    public Nullable<int> CreatedByContactId { get; set; }
    public const string Schema_CreatedDateTime = "CreatedDateTime";
    public Nullable<System.DateTime> CreatedDateTime { get; set; }
    public const string Schema_RevisedByContactId = "RevisedByContactId";
    public Nullable<int> RevisedByContactId { get; set; }
    public const string Schema_RevisedDateTime = "RevisedDateTime";
    public Nullable<System.DateTime> RevisedDateTime { get; set; }
    public const string Schema_VerifiedByContactId = "VerifiedByContactId";
    public Nullable<int> VerifiedByContactId { get; set; }
    public const string Schema_VerifiedDate = "VerifiedDate";
    public Nullable<System.DateTime> VerifiedDate { get; set; }
    public const string Schema_PayLevel = "PayLevel";
    public string PayLevel { get; set; }
    public const string Schema_CompensationComments = "CompensationComments";
    public string CompensationComments { get; set; }
    public const string Schema_DisplayAs = "DisplayAs";
    public string DisplayAs { get; set; }
    public const string Schema_JobFunction = "JobFunction";
    public string JobFunction { get; set; }
    public const string Schema_ImportSource = "ImportSource";
    public string ImportSource { get; set; }
    public const string Schema_EmployeeCategory = "EmployeeCategory";
    public string EmployeeCategory { get; set; }
    public const string Schema_IsPrincipal = "IsPrincipal";
    public Nullable<bool> IsPrincipal { get; set; }
    public const string Schema_EarnsCommission = "EarnsCommission";
    public Nullable<bool> EarnsCommission { get; set; }
    public const string Schema_ReportsToContactId = "ReportsToContactId";
    public Nullable<int> ReportsToContactId { get; set; }
    public const string Schema_PersonnelComments = "PersonnelComments";
    public string PersonnelComments { get; set; }
    public const string Schema_ImportSourceId = "ImportSourceId";
    public Nullable<int> ImportSourceId { get; set; }
    public const string Schema_ImportTaskId = "ImportTaskId";
    public Nullable<int> ImportTaskId { get; set; }
    public const string Schema_EducationComments = "EducationComments";
    public string EducationComments { get; set; }
    public const string Schema_ExperienceComments = "ExperienceComments";
    public string ExperienceComments { get; set; }
    public const string Schema_QualificationComments = "QualificationComments";
    public string QualificationComments { get; set; }
    public const string Schema_IsActive = "IsActive";
    public bool IsActive { get; set; }

    public virtual ICollection<HrCompensation> HrCompensation { get; set; }
    public virtual Contact Contact { get; set; }
    public virtual Contact VerifiedByContact { get; set; }
    public virtual Contact CreatedByContact { get; set; }
    public virtual Contact RevisedByContact { get; set; }
    public virtual Contact ReportsToContact { get; set; }
    public virtual ICollection<HrSplitPlan> HrSplitPlan { get; set; }
}
}
当sql查询返回时,它将向查询中添加一个EXISTS子句,并且EXISTS中的筛选器将返回
HrCompensation
中的所有记录(如果它在EXISTS中至少获得一条记录)

我想过滤整个查询,但不想添加Exist。如果我这样做

query = query.Where(x => x.HrCompensation
                          .Where(y => y.TransactionDate >= TransactionDate.Min));
它将返回一个错误

IQueryable无法转换为bool


有人能提出任何建议吗?…

列表。其中
不返回bool<代码>列表。任何都可以。在
Where
子句中,它检查是否为每个记录设置了条件,并将其包括在结果列表中!但在
Any
中,如果只找到一条具有该条件的记录,则返回true。所以,是的,它应该给你这个错误。您可以使用以下选项:

query = query.Where(
        x => x.HrCompensation.Where(
        y => y.TransactionDate >= TransactionDate.Min).FirstOrDefault() != null);

定义整个查询的确切含义。输出应该是什么样的?意味着我想在lambda生成的整个查询中使用我的过滤器。它在HrCompensation上实现“交叉应用”,然后在Hr.Id=HrCompensation.HrId上与Hr进行左外连接,因为我也在查询中包含“HrCompensation”。我需要过滤在何处条件时,查询申请加入人力资源和人力资源薪酬。以便准确返回记录使用
选择
筛选记录。我不能给你一般的指导。写一个代码,并指出你想要如何得到结果,如果出现任何错误,请在这里发布。谢谢你的评论,问题已经解决了。在我的情况下,我不想使用select。现在我把结构颠倒过来。我已将Hr用作HrCompensation内部的导航属性,从那里我也可以访问我的联系人。无论如何,谢谢你的时间。
namespace REApplications.BLL.Parameters
{
using System.Linq;

public class  HrSearchParameters : ISearchParameters<Hr>
{

    public int? ContactId;
    public HrCompensationSearchParameters HrCompensation = null;

    /// <summary>
    /// Bulid the query based on search params
    /// </summary>
    /// <param name="query">Apply filters to the this parameter</param>
    /// <returns></returns>
    public IQueryable<BLL.Hr> ToFilteredExpression(IQueryable<BLL.Hr> query)
    {
        if (ContactId.HasValue)
        {
            query = query.Where(x => x.ContactId == ContactId);
        }

        if (HrCompensation != null)
        {
            query = HrCompensation.ToFilteredExpression(query);
        }

        return query;
    }

}
}
namespace REApplications.BLL.Parameters
{
using System.Linq;

public class HrCompensationSearchParameters : ISearchParameters<HrCompensation>
{
    public DateRangeSearchParameter TransactionDate = null;
     /// <summary>
    /// Checks each search parameter and returns the built query expression.
    /// </summary>
    /// <param name="query">The query to which the filters will be applied.</param>
    /// <returns></returns>
    /// 
    public IQueryable<BLL.Hr> ToFilteredExpression(IQueryable<BLL.Hr> query)
    {
        if (TransactionDate != null)
        {
            if (TransactionDate.Min.HasValue)
            {
                query = query.Where(x => (x.HrCompensation.Any(hr => hr.TransactionDate >= TransactionDate.Min)));
            }

            if (TransactionDate.Max.HasValue)
            {
                query = query.Where(x => (x.HrCompensation.Any(hr => hr.TransactionDate <= TransactionDate.Max)));
            }
        }
    return query;
    }
    public IQueryable<BLL.HrCompensation> ToFilteredExpression(IQueryable<BLL.HrCompensation> query)
    {
        if (TransactionDate != null)
        {
            if (TransactionDate.Min.HasValue)
            {
                query = query.Where(x => (x.TransactionDate >= TransactionDate.Min));
            }

            if (TransactionDate.Max.HasValue)
            {
                query = query.Where(x => (!x.TransactionDate.HasValue) || (x.TransactionDate <= TransactionDate.Max));
            }
        }
        return query;
    }
}
}
query = query.Where(x => (x.HrCompensation
                           .Any(hr => hr.TransactionDate >= ransactionDate.Min)));
query = query.Where(x => x.HrCompensation
                          .Where(y => y.TransactionDate >= TransactionDate.Min));
query = query.Where(
        x => x.HrCompensation.Where(
        y => y.TransactionDate >= TransactionDate.Min).FirstOrDefault() != null);