如何使用distinct关键字过滤对象

时间:2012-10-22 11:28:33

标签: asp.net-mvc-3 nhibernate

我有一个项目列表,我可以获得有关项目的所有详细信息。在项目类中,我在项目中有客户对象,我只想根据客户过滤该列表。我怎么能这样做。

public ICollection<Project> GetProjectBasicDetailsByProjectTypeCustomerID(ProjectType projectType, string custName, string cordinatorName, string projectName)
    {
        oLog.Debug("Started");
        ISession session = DataAccessLayerHelper.OpenReaderSession();
        ITransaction transaction = null;
        ICollection<Project> projectList = null;
        try
        {
            transaction = session.BeginTransaction();
            ICriteria criteria = session.CreateCriteria(typeof(Project),"Project")
                .CreateAlias("Project.customer","customer",NHibernate.SqlCommand.JoinType.InnerJoin)
                .CreateAlias("Project.Coordinator", "Coordinator", NHibernate.SqlCommand.JoinType.InnerJoin)
                .Add(Restrictions.Eq("Project.ProjectType", projectType));


            projectList = criteria.List<Project>().ToList();

            session.Flush();
            transaction.Commit();
        }
        catch (Exception ex)
        {
            if (transaction != null && transaction.IsActive)
                transaction.Rollback();
            oLog.Error(ex);
        }
        finally
        {
            if (transaction != null)
                transaction.Dispose();

            if (session != null && session.IsConnected)
                session.Close();
        }
        oLog.Debug("End");
        return projectList;
    }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望从您的项目中获得独特的客户?

在这种情况下,我认为这样的事情应该有效(这应该给你这个标准的dictinct项目ID - 也许有办法直接获得项目 - 但我现在无法测试:

ICriteria criteria = session.CreateCriteria(typeof(Project),"Project")
                .SetProjection(Projections.Distinct(Projections.Property("project.Id")))
                .CreateAlias("Project.customer","customer",NHibernate.SqlCommand.JoinType.InnerJoin)
                .CreateAlias("Project.Coordinator", "Coordinator", NHibernate.SqlCommand.JoinType.InnerJoin)
                .Add(Restrictions.Eq("Project.ProjectType", projectType));