Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:50:41+00:00 2026-05-16T20:50:41+00:00

I am using the Specification pattern, and have a working implementation (taken from the

  • 0

I am using the Specification pattern, and have a working implementation (taken from the WhoCanHelpMe Codeplex project) for getting data via NLinq, generic repositories and all that goodness.

The root method is:

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

The FindAll() method does the following:

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

And, SatisfyingElementsFrom() does this:

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

So, for querying cases by CaseNb property of a Case, it’s pretty straight-forward. A Specification like the one below works for me and gets the cases I’d want.

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

}

However, I am at a loss to understand how to do this when crossing multiple entities. What I’d like to have is a Specification that allows me to get Cases by UserName. Basically, in the database, there are three tables and these have been carried into entities. Here’s are entities:

Here’s the Case class:

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

Here’s the CaseUser:

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

And, User:

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

How would I write the Expression to get the data across the association table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:50:43+00:00Added an answer on May 16, 2026 at 8:50 pm

    I believe your specification implementation should look something like this:

    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); } 
        } 
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using DBus in a project. I understand from DBus specification that for
Using Flex and Bison, I have a grammar specification for a boolean query language,
I'm using a DocumentBuilder to parse XML files. However, the specification for the project
I have been working on my first Java project with the 'Drools Planner' package,
I have recently taken up the activity of parsing binary data with Python but
When plotting multiple data series using both line specification ( X , Y ,
I have a binary file specification that describes a packetized data structure. Each data
I am thinking of using Specification pattern for validation purposes. The hard thing is
In the project I am working on, we are passing a specification (ala the
I've been looking into the specification pattern for my repositories, I'm using EF4 inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.