views:

69

answers:

3

I have two tables Boxer and Prospect. Boxers has general stuff like name and and dob etc and a BoxerId While Prospect contains only one value (at the moment) which is a boxerId. If a boxer is a prospect(up and coming boxer) there Id will be in the prospect table.

This works fine but now I want to select all boxers that are prospects

    public static IQueryable<Boxer> IsProspect(this IQueryable<Boxer> query)
    {
        //this does not filter down to only prospects!!!
        return query.Where(x => x.Prospect != null);
    }

This is the function I call using:

var repository = GetRepository<Boxer>();
var boxers = repository.Query().IsProspect();

I would hope this would filter my collection of all boxers down to just boxers that are prospects!

Oddly it doesnt filter it but if i hover over my boxers object and look at each boxer during debugging I can see "IsProspect" true or false correctly IsProspect Debug Example

+1  A: 

If you want only those objects where IsProspect is true then use that as your predicate:

public static IQueryable<Boxer> IsProspect(this IQueryable<Boxer> query)
{
    return query.Where(x => x.IsProspect);
}
Mark Byers
That fails to work with the error/no values returned!{"could not resolve property: IsProspect of: BensBoxing.Domain.Boxer"}
Steve
A: 

It sounds like you should use table-per-subclass inheritance.

Stephen Cleary
i dont really know what im looking at here any help?
Steve
From your problem description, it sounds like (conceptually) you have a `Boxer` domain object, and a `Prospect` derived domain object. That is, the `Prospect` class is derived from `Boxer`. I re-read your question and realized you use NHibernate and not LINQ to Entities, so I changed the link to point to the relevant NH docs.
Stephen Cleary
A: 

overload a datacontext and directly call the function from your datacontext object

public partial class myDataContext
{
       public IQueryable<Boxer> IsProspect()
       {
             return from tBoxer in myDataContext.tBoxer 
                    where tBoxer.IsProspect == true             
                    select tBoxer;
       }
}

and you would call it like a normal function call like so var db = new myDataConext(); var prospects = db.IsProspect();

Ayo