使用NHibernate获取两列的计数

使用NHibernate获取两列的计数,nhibernate,Nhibernate,我需要使用NHibernate查询获得两个字段的计数。给定下面historicalList的示例数据,结果应该是1个项目分配给2个节点。如何使用Nhibernate查询获得结果。请看下面我的方法,有人能帮我重写代码吗 //Sample Data var nodelist= new List<Node>{ new Node{1, "Node1"}, new Node{2, "Node2"}, new Node{3, "Node3"}, new Node{4, "Node4"}, new

我需要使用NHibernate查询获得两个字段的计数。给定下面historicalList的示例数据,结果应该是1个项目分配给2个节点。如何使用Nhibernate查询获得结果。请看下面我的方法,有人能帮我重写代码吗

//Sample Data
var nodelist= new List<Node>{
new Node{1, "Node1"},
new Node{2, "Node2"},
new Node{3, "Node3"},
new Node{4, "Node4"},
new Node{5, "Node5"},
}

var projectlist= new List<Project>{
new Project{1, "Project1"},
new Project{2, "Project2"},
new Project{3, "Project3"},
new Project{4, "Project4"},
new Project{5, "Project5"},
}

var historicalList= new List<Historical>
{
 new Historical{1, 1,1}
 new Historical{1, 1,2}
}



public class Node
    {
        public virtual long ID { get; set; }
        public virtual string NodeName { get; set; }
    }

 public class Project
    {
        public virtual long ID { get; set; }
        public virtual string ProjectName { get; set; }
    }


public Historical
{
        public virtual long ID { get; set; }
        public virtual string ProjectID { get; set; }
        public virtual string NodeID { get; set; }     
}


//sample code 

 using (var session = OpenSession())
            {
                var historical= session.Query<Historical>()
              .Where(
                  x => nodeIds.Contains(x.Node.ID));
                var nodeCount = historical.Select(y => y.Node.ID).Distinct().Count();
                var projectCount = historical.Select(y => y.Project.ID).Distinct().Count();


            }

这里有一种不使用DISTINCT的替代方法

var historical   = session.Query<Historical>().Where(x => /* other filters here*/ );

var nodeCount    = session.Query<Node>()
                   .Where(n => historical.Any(h => h.NodeId == n.NodeId)).Count();

var projectCount = session.Query<Project>()
                   .Where(p => historical.Any(h => h.ProjectId == p.ProjectId)).Count();
要在一次往返中执行两次计数,请使用,它现在内置在最新的NHibernate上

var historical   = session.Query<Historical>().Where(x => /* other filters here*/ );

var nodeCount    = session.Query<Node>()
                   .Where(n => historical.Any(h => h.NodeId == n.NodeId))
                   .ToFutureValue(f => f.Count());

var projectCount = session.Query<Project>()
                   .Where(p => historical.Any(h => h.ProjectId == p.ProjectId))
                   .Count();
注意,您无法看到这两条语句是否通过SQL Server profiler执行一次往返,您必须使用。如果您不能使用NHProf,只需使用和不使用ToFutureValue对查询进行基准测试


另外,请将Where+Any方法与Distinct方法进行比较,看看Where+Any是否更快,否则只需使用Distinct方法。

这里有一种不使用Distinct的替代方法

var historical   = session.Query<Historical>().Where(x => /* other filters here*/ );

var nodeCount    = session.Query<Node>()
                   .Where(n => historical.Any(h => h.NodeId == n.NodeId)).Count();

var projectCount = session.Query<Project>()
                   .Where(p => historical.Any(h => h.ProjectId == p.ProjectId)).Count();
要在一次往返中执行两次计数,请使用,它现在内置在最新的NHibernate上

var historical   = session.Query<Historical>().Where(x => /* other filters here*/ );

var nodeCount    = session.Query<Node>()
                   .Where(n => historical.Any(h => h.NodeId == n.NodeId))
                   .ToFutureValue(f => f.Count());

var projectCount = session.Query<Project>()
                   .Where(p => historical.Any(h => h.ProjectId == p.ProjectId))
                   .Count();
注意,您无法看到这两条语句是否通过SQL Server profiler执行一次往返,您必须使用。如果您不能使用NHProf,只需使用和不使用ToFutureValue对查询进行基准测试

另外,请将Where+Any方法与Distinct方法进行基准测试,并查看Where+Any是否更快,否则只需使用Distinct方法。

您可以直接在Linq to NHibernate API上使用join,即使您没有使用QueryOver的规则映射关系

即使没有使用QueryOver的常规映射关系,也可以直接在Linq到NHibernate API上使用join