سؤال

أنا أستخدم نمط المواصفات ، ولدي تطبيق عمل (مأخوذ من مشروع WhocanHelpme Codeplex) للحصول على البيانات عبر NlinQ ، المستودعات العامة وكل هذا الخير.

طريقة الجذر هي:

public IList<Case> GetCasesByUsername(string username)
{
    CaseByUserNameSpecification spc = new CaseByUserNameSpecification(username);
    return this.caseRepository.FindAll(spc).ToList();
}

طريقة findall () تفعل ما يلي:

public IQueryable<T> FindAll(ILinqSpecification<T, T> specification)
{
    return specification.SatisfyingElementsFrom(this.Session.Linq<T>());
}

و FresidentingElementsfrom () يفعل هذا:

public virtual IQueryable<TResult> SatisfyingElementsFrom(IQueryable<T> candidates)
{
    if (this.MatchingCriteria != null)
    {
        return candidates.Where(this.MatchingCriteria).ToList().ConvertAll(this.ResultMap).AsQueryable();
    }

    return candidates.ToList().ConvertAll(this.ResultMap).AsQueryable();
}

لذلك ، بالنسبة للاستعلام عن الحالات من قبل ملكية Casenb لحالة ما ، فهي مستقيمة إلى الأمام. مواصفات مثل المواصفات أدناه تعمل بالنسبة لي وتحصل على الحالات التي أريدها.

public class CaseByCaseNbSpecification : QuerySpecification<User>
{
    private string caseNb;

    public CaseByCaseNbSpecification(string caseNb)
    {
        this.caseNb = caseNb;
    }

    public string UserName
    {
        get { return this.caseNb; }
    }

    public override Expression<Func<Case, bool>> MatchingCriteria
    {
        get { return u => u.CaseNb.Equals(this.caseNb, StringComparison.CurrentCultureIgnoreCase); }
    }

}

ومع ذلك ، أنا في حيرة لفهم كيفية القيام بذلك عند عبور كيانات متعددة. ما أود الحصول عليه هو مواصفات تتيح لي الحصول على الحالات حسب اسم المستخدم. في الأساس ، في قاعدة البيانات ، هناك ثلاثة جداول وقد تم نقلها إلى كيانات. هنا كيانات:

ها هي فئة القضية:

public class Case : Entity
{
    private ICollection<CaseUser> caseUsers = new HashSet<CaseUser>();

    public virtual Patient Patient { get; set; }
    public virtual string CaseNb { get; set; }
    ...
    public virtual IEnumerable<CaseUser> CaseUsers { get { return caseUsers; } }
}

ها هي CaseUser:

public class CaseUser : Entity
{
    public virtual Case Case { get; set; }
    public virtual User User { get; set; }
    ...
}

والمستخدم:

public class User : Entity
{
    private ICollection<CaseUser> caseUsers = new HashSet<CaseUser>();

    public virtual Account Account { get; set; }
    public virtual string UserName { get; set; }
    ...
    public virtual IEnumerable<CaseUser> CaseUsers { get { return caseUsers; } }
}

كيف يمكنني كتابة التعبير للحصول على البيانات عبر جدول الجمعية؟

هل كانت مفيدة؟

المحلول

أعتقد أن تطبيق المواصفات الخاص بك يجب أن يبدو مثل هذا:

public class CaseByUsernameSpecification : QuerySpecification<Case> 
{ 
    private string userName; 

    public CaseByUsernameSpecification(string userName) 
    { 
        this.userName = userName; 
    } 

    public string UserName 
    { 
        get { return this.userName; } 
    } 

   public override Expression<Func<Case, bool>> MatchingCriteria 
    { 
        get { return c => c.CaseUsers.Any(cu => cu.User.Username == this.userName); } 
    } 

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top