开发者

NHibernate QueryOver<> problem return a parent collection of a child entity

I have a parent entity Category_Types with a collection of child entities Categories each one of these child entities has a collection of child ent开发者_Go百科ities Expenses:

Category_Types >> Categories (1:n), Categories >> Expenses (1:n)

i want to query for the total expenses for a specific Category_Type between specific dates into the following unmapped class

public class EntityTotals<T>
{
    T _Entity;
    public T Entity
    {
        get
        {
            return _Entity;
        }
        set
        {
            _Entity = value;
        }
    }
    decimal _Total;
    public decimal Total
    {
        get
        {
            return _Total;
        }
        set
        {
            _Total = value;
        }
    }
}

i have the following sql query:

select ct.Cat_Type , SUM(isnull(e.Spends,0)) from Expenses e right join Categories c    on e.Category_Id = c.Category_Id
right join Category_Types ct on ct.Cat_Type_Id = c.Cat_Type_Id
where e.Spend_Date between @from and @to
group by ct.Cat_Type 

so i wrote a query using QueryOver<> to get the same results of the sql query and i get the results into EntityTotals<> class as following:

Expenses e = null;
Categories c = null;
Category_Types ct = null;
return Session.QueryOver<Expenses>((() => e))
    .JoinAlias(() => e.Category, () => c)
    .JoinAlias(() => c.Category_Type, () => ct)
    .WhereRestrictionOn(() => e.Spend_Date)
    .IsBetween(from)
    .And(to)
    .SelectList(list => list
        .SelectGroup(() => ct)
        .SelectSum(ee => ee.Spends))
        .List<object[]>()
        .Select(exp =>
            new EntityTotals<Categories>()
            {
                Entity = (Categories)exp[0],
                Total = (decimal)exp[1]
            })
            .ToList<EntityTotals<Categories>>();

when i tested this query it gave me the following exception:

could not resolve property: ct of: Expenses

so i tried to get only some properties of Category_Types into the following unmapped class

public class Totals
{
    int _Id;
    public int Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }
    decimal _Total;
    public decimal Total
    {
        get
        {
            return _Total;
        }
        set
        {
            _Total = value;
        }
    }
}

with the following query to only get the property Cat_Type_Id of Category_Types and it works fine:

Expenses e = null;
Categories c = null;
Category_Types ct = null;
return Session.QueryOver<Expenses>((() => e))
      .JoinAlias(() => e.Category, () => c)
      .JoinAlias(() => c.Category_Type, () => ct)
      .WhereRestrictionOn(() => e.Spend_Date)
      .IsBetween(from)
      .And(to)
      .SelectList(list => list
          .SelectGroup(() => ct.Cat_Type_Id)
          .SelectSum(ee => ee.Spends))
          .List<object[]>()
          .Select(exp =>
              new Totals()
              {
                  Id = (int)exp[0],
                  Total = (decimal)exp[1]
              })
              .ToList<Totals>();

so how can i get the complete object of Category_Types from the first query ?

Thanks ;


Depending on what you actually want to return, one of the following solutions will work:

1) If you want to return EntityTotals<Categories> do this:

.SelectList(list => list
    .SelectGroup(() => e.Category)
    .SelectSum(ee => ee.Spends))
    .List<object[]>()
    .Select(exp =>
        new EntityTotals<Categories>()
        {
            Entity = (Categories)exp[0],
            Total = (decimal)exp[1]
        })
        .ToList<EntityTotals<Categories>>();

2) If you want to return EntityTotals<Category_Types> do this:

.SelectList(list => list
    .SelectGroup(() => c.Category_Type)
    .SelectSum(ee => ee.Spends))
    .List<object[]>()
    .Select(exp =>
        new EntityTotals<Category_Types>()
        {
            Entity = (Category_Types)exp[0],
            Total = (decimal)exp[1]
        })
        .ToList<EntityTotals<Category_Types>>();

You cannot do .SelectGroup(() => ct) because ct is simply not a property of anything. That's what the exception says.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜