views:

596

answers:

1

Short Version This query works in the database but fails with Linq To NHibernate. Why?

var items = (from g in db.Find<DataGroupInfo>() 
              where (from d in g.data where d.Id == dataID select d).Count() > 0  
              select g).ToList();

Detailed Long Version

I have two objects mapped by NHibernate Automapper with a ManyToMany relationship.

return Fluently.Configure()
     //a few other settings here...
     //relevant settings below
               .Override<DataGroupInfo>(map =>
               {
                   map.HasManyToMany(d => d.data);
               }
               .Conventions.Add(DefaultCascade.All())
               .Conventions.Add(DefaultLazy.Never())


public class DataInfo
{
    public virtual int Id { get; private set; }
    public virtual DateTime created { get; set; }
    public virtual string label { get; set; }
    public virtual string description { get; set; }
}

public  class DataGroupInfo
{
    public virtual Int32 Id { get; set; }

    public virtual DateTime created { get; set; }
    public virtual string label { get; set; }
    public virtual string description { get; set; }

    public virtual IList<DataInfo> data { get; set; }
}

I want to grab all the DataGroups that contain a certain DataInfo.

This query works in SQLite admin so I think my database is properly setup:

select * from DataGroupInfo
INNER JOIN DataInfoToDataGroupInfo ON
DataGroupInfo.Id ==  DataInfoToDataGroupInfo.DataGroupInfo_id
where
DataInfoToDataGroupInfo.DataInfo_id == 3

I'm a repository that exposes Linq in these two ways

public IQueryable<T> Find<T>()
{
    return session.Linq<T>();
}

public IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate)
{
    return Find<T>().Where(predicate);
}

I use the repositories like this

  static public List<DataGroupInfo> GetAllWithData(Int32 dataID)
    {
        using (var db = new DBRepository())
        {
            //var items = (from g in db.Find<DataGroupInfo>() 
            //                where (from d in g.data where d.Id == dataID select d).Count() > 0  
            //             select g).ToList();

            var items = db.Find<DataGroupInfo>(dg => dg.data.Where(d => d.Id == dataID).Count() > 0 ).ToList();

            return items;
        }
    }

When the Linq tries to execute above I get the following error under either method:

NHibernate.QueryException was unhandled by user code
Message="could not resolve property: data.Id of: MapLarge.Public.Data.DataGroupInfo"
Source="NHibernate" StackTrace: at NHibernate.Persister.Entity.AbstractPropertyMapping.GetColumns(String propertyName)

What am I doing wrong?

----Update-----

This work around gives the expected result, but its a brute force approach pulling every datagroup into memory and then filtering objects using c# rather than the database.. but at least it narrows down my problem to a NHibernate specific issue.

        var step1 = db.Find<DataGroupInfo>().ToList();
        var items = step1.Where(dg => dg.data.Where(d => d.Id == dataID).Count() > 0).ToList();

I'd still really love an answer if anyone is out there :-)

+2  A: 

LINQ-to-NHibernate currently does not support subqueries or joins: http://ayende.com/Blog/archive/2009/07/26/nhibernate-linq-1.0-released.aspx

afsharm
Thank you. I thought I remembered a limitation like that but wasn't sure if that limit had changed since last summer. Nice when that feature works.
Glenn
Glenn