tags:

views:

101

answers:

2

Code

IList VendorList;

                VendorList = session
                    .CreateCriteria<VendorLookup>()
                    .Add(Expression.Eq("NetworkRunId", networkRunId))
                    .Add(Expression.Not(Expression.Eq("VendorName", " ")))  
                    .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
                    .AddOrder(new Order("VendorName", true))
                    .List<IVendorLookup>().Distinct<IVendorLookup>().ToList();

Generated Query Please help me out

SELECT   this_.Sp    as HCO1_25_0_,
         this_.NID  as Network2_25_0_,
         this_.Vname as HCO3_25_0_
FROM     HCO_V_Lookup this_
WHERE    this_.NID = 5 /* @p0 */
         and not (this_.VNAME = ' ' /* @p1 */)
ORDER BY this_.VNAME asc
A: 

I presume you're wondering why the DISTINCT doesn't appear in the SQL. It's because it's applied in your criteria query as a result transformer - the SQL query results are filtered to make them distinct after the query has been run.

David M
i changed my code and now i am able tro see the distinct in the queryadded this.SetProjection(Projections.Distinct(Projections.ProjectionList().Add(Projections.Property("NetworkRunId")).Add(Projections.Property("VendorName"))))removed .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer()i am getting an exceptionERROR: The value "System.Object[]" is not of type "Common.Interface.Domain.Common.IVendorLookup" and cannot be used in this generic collection.Parameter name: value
bharat
i found the issue is when the query is generated with DISTINCT i am getting exception.
bharat
if i use this i am getting only one result setcrit.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());
bharat
Suggest you edit your question to make it clearer. I can't read all your comments above, as they extend under a list on the right hand side of the screen.
David M
A: 

ICriteria criteria = session.CreateCriteria(typeof(Person)); criteria.SetProjection( Projections.Distinct(Projections.ProjectionList() .Add(Projections.Alias(Projections.Property("FirstName"), "FirstName")) .Add(Projections.Alias(Projections.Property("LastName"), "LastName"))));

criteria.SetResultTransformer( new NHibernate.Transform.AliasToBeanResultTransformer(typeof(Person)));

IList people = criteria.List();

bharat