2

DTOにコレクションプロパティを設定する必要がありますが、これを行うための情報を見つけるのに問題があります。

私はこのようにそれをやろうとしました:

ICriteria selectCriteria = Session.CreateCriteria<DataRun>()
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("SomeCollection"), "Collection"))
            .SetResultTransformer(Transformers.AliasToBean<MyDto>());

ただし、MyDto.Collectionは常にnullです。私はこれを間違ってやっていますか、これは可能ですか?

また、私はもともとSubQueryでこれを行うことを計画していたので、DTOのコレクションを他のDTOで埋めることができましたが、サブクエリの結果に複数の行があり(必要に応じて)、Sqlitがそれを好まないため、これは機能しません(例外をスローします)。ここで行うべき正しいことは何ですか?

4

2 に答える 2

0

次の構文を使用して、プロジェクトのコレクション プロパティを取得しました。

public IList<ChildCollectionType> GetChildObjectsByParentId(Int32 id)
{
    ISession session = GetSession();//Get NHibernate session routine
    return session.Load<ParentDTO>(id).ChildCollectionProperty;
}

ここで、idは親オブジェクトのキーです。

于 2011-04-19T08:27:02.463 に答える
0

カスタム変換を使用できます。

ICriteria selectCriteria = Session.CreateCriteria<DataRun>()
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("SomeCollection"), "Collection"))
            .TransformUsing(new CustomTransformer());

これがカスタムトランスフォーマーの実装です

public class CustomTransformer : IResultTransformer
    {
        public System.Collections.IList TransformList(System.Collections.IList collection)
        {
            return collection;
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
            return new MyDto
            {
                //map your data to dto and convert to data type if needed
                YourProperty1 = tuple[0],
                YourProperty12 = (int)tuple[1]
            };
        }
    }
于 2016-11-26T12:31:56.140 に答える