子エンティティのコレクションを持つ親エンティティ Category_Types があります。これらの子エンティティのそれぞれに、子エンティティ Expenses のコレクションがあります。
Category_Types >> カテゴリ (1:n)、カテゴリ >> 経費 (1:n)
特定の日付間の特定の Category_Type の総費用を、次のマップされていないクラスにクエリしたい
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;
}
}
}
私は次のSQLクエリを持っています:
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
そのため、QueryOver<> を使用してクエリを作成し、SQL クエリと同じ結果を取得し、次のように結果を EntityTotals<> クラスに取得しました。
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>>();
このクエリをテストすると、次の例外が発生しました。
プロパティを解決できませんでした: ct of: Expenses
そのため、Category_Types の一部のプロパティのみを次のマップされていないクラスに取得しようとしました
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;
}
}
}
Category_Types のプロパティ Cat_Type_Id のみを取得する次のクエリを使用すると、正常に動作します。
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>();
最初のクエリから Category_Types の完全なオブジェクトを取得するにはどうすればよいですか?
ありがとう ;