簡體   English   中英

ICollection類型的訪問導航屬性

[英]Access Navigation Property of type ICollection

我有兩個實體類

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

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

public partial class HrCompensation
{
    public const string Schema_Id = "Id";
    public int Id { get; set; }
    public const string Schema_HrId = "HrId";
    public Nullable<int> HrId { 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_TransactionDate = "TransactionDate";
    public Nullable<System.DateTime> TransactionDate { get; set; }
    public const string Schema_Title = "Title";
    public string Title { get; set; }
    public const string Schema_Occupation = "Occupation";
    public string Occupation { get; set; }
    public const string Schema_Specialty = "Specialty";
    public string Specialty { get; set; }
    public const string Schema_EmployeeClassification = "EmployeeClassification";
    public string EmployeeClassification { get; set; }
    public const string Schema_PayRate = "PayRate";
    public Nullable<decimal> PayRate { get; set; }
    public const string Schema_PayFrequency = "PayFrequency";
    public string PayFrequency { get; set; }
    public const string Schema_Comments = "Comments";
    public string Comments { get; set; }
    public const string Schema_SplitPackage = "SplitPackage";
    public string SplitPackage { get; set; }
    public const string Schema_CommissionScheduleComments = "CommissionScheduleComments";
    public string CommissionScheduleComments { get; set; }
    public const string Schema_LeaveOfAbsenceStartDate = "LeaveOfAbsenceStartDate";
    public Nullable<System.DateTime> LeaveOfAbsenceStartDate { get; set; }
    public const string Schema_LeaveOfAbsenceEstimatedReturnDate = "LeaveOfAbsenceEstimatedReturnDate";
    public Nullable<System.DateTime> LeaveOfAbsenceEstimatedReturnDate { get; set; }
    public const string Schema_LeaveOfAbsenceActualReturnDate = "LeaveOfAbsenceActualReturnDate";
    public Nullable<System.DateTime> LeaveOfAbsenceActualReturnDate { get; set; }
    public const string Schema_TerminationDate = "TerminationDate";
    public Nullable<System.DateTime> TerminationDate { get; set; }
    public const string Schema_VacationRemaining = "VacationRemaining";
    public Nullable<decimal> VacationRemaining { get; set; }
    public const string Schema_TerminationReason = "TerminationReason";
    public string TerminationReason { get; set; }
    public const string Schema_TerminationComments = "TerminationComments";
    public string TerminationComments { 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_ImportSource = "ImportSource";
    public string ImportSource { get; set; }
    public const string Schema_FiscalStartMonth = "FiscalStartMonth";
    public Nullable<byte> FiscalStartMonth { 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_BonusPercent = "BonusPercent";
    public Nullable<decimal> BonusPercent { get; set; }
    public const string Schema_BonusAmount = "BonusAmount";
    public Nullable<decimal> BonusAmount { get; set; }
    public const string Schema_BonusFrequency = "BonusFrequency";
    public string BonusFrequency { get; set; }

    public virtual Hr Hr { get; set; }
    public virtual Contact VerifiedByContact { get; set; }
}
}

然后我有一個Repository類

namespace REApplications.BLL.Repositories
{
using System.Data.Entity;
using System.Linq;
using System;
using REApplications.Common.User;
using REApplications.BLL.ViewModels;

public class HrRepository : BaseRepository<Hr>, IHrRepository
{
    #region Setup
    protected override IDbSet<Hr> DbSet { get { return DbContext.Hr; } }
    /// <summary>
    /// Use this contructor when a dbContext doesn't exist.
    /// This will create a dbContext object and use that to persist data.
    /// This constructor is also important for the repository to show up as a data source when using for SSRS reports.
    /// </summary>
    public HrRepository() : base() { }

    /// <summary>
    /// Use this constructor to pass an existing dbContext.
    /// This is useful if multiple operations to the database need to be batched
    /// together in a single operation.
    /// </summary>
    /// <param name="dbContext"></param>
    public HrRepository(IDataModelContext dbContext, Identity identity) : base(dbContext, identity) { }




    Public IQueryable<Hr> SearchAll
    (
        Parameters.ISearchParameters<Hr> searchParams = null
        , string sortBy = ProjectConstants.DefaultSortKey
        , System.Data.SqlClient.SortOrder sortDirection = System.Data.SqlClient.SortOrder.Ascending
    )
    {
        var query = base.SearchAll()
            .Where(hr => hr.IsActive)
            .AsQueryable();

        // Apply search filtering
        if (searchParams != null)
        {
            query = searchParams.ToFilteredExpression(query);
        }

        // Apply sorting
        query = query.DynamicOrderBy(sortBy, sortDirection);

        return query;
    }




}
}

我在其中查詢= searchParams.ToFilteredExpression(query);

並像這樣應用搜索過濾器

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

在我正在做的HrCompensationSearchParameters文件中

query = query.Where(x => (x.HrCompensation
                           .Any(hr => hr.TransactionDate >= ransactionDate.Min)));

通過它,當sql查詢返回時,它將在查詢中添加一個EXISTS子句,並且在EXISTS中添加一個過濾器,如果它在EXISTS中至少獲得一條記錄,它將從HrCompensation返回所有記錄。

我想過濾整個查詢,不想添加Exist。 如果我做

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

它將返回一個錯誤

IQueryable無法轉換為bool

有人可以提出任何建議嗎?

List.Where不返回布爾值。 List.Any Where子句中,它檢查是否為每個記錄創建了條件,並將其包括在結果LIST中! 但是在Any ,如果它僅找到一個具有該條件的記錄,則返回true。 是的,它應該給您該錯誤。 您可以使用此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM