I have the classes Foo and Bar:
public class Foo
{
public int Id {get;set;}
public IEnumerable<Bar> Bars {get;private set;}
}
public class Bar
{
public int Id {get;set;}
public string Type {get;set;}
public string Description {get;set;}
}
These two classes map to tables in the database in a predictable ‘Bar has foreign key to Foo‘ kind of way. (I can provide the FluentNHibernate mappings if required).
I would like to write a linq query that does the equivalent of this:
SELECT FOOS.*, (SELECT Description FROM BARS WHERE BAR.FOOID = FOO.ID AND BAR.TYPE = 'Irish')
FROM FOOS
WHERE FOO.ID = 1
How would I do this?
And yes, I’m aware that the query will break if the Foo has more than one Irish Bar (although I could use TOP 1 type syntax to protect it). And yes, I mean to use a subquery rather than a join.
EDIT:
This is what I’m trying so far:
var foosWithIrishBarDescription = FooRepository.All.Where(x => x.Id == 1).Select(x=> new {FooId = x.Id, IrishBarDescription = x.Bars.Where(y => y.Type == "Irish").Select(y => y.Description)});
But I get the error ‘The method ‘Select’ is not implemented.’ Can this not be achieved in Linq to NHibernate? (I’m using 2.1).
I assume your IEnumerable is actually a property called “Bars”: