使用NHibernate获取两列的计数

时间:2015-07-27 06:59:08

标签: nhibernate

我需要使用NHibernate查询获取两个字段的计数。给定下面的历史列表的样本数据,结果应该是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();


            }

2 个答案:

答案 0 :(得分:2)

这是一种不使用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();

要在一次往返中执行两次计数,请使用ToFutureValue,它现在内置于最新的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探查器查看两个语句是否执行了一次往返,您必须使用NHProf。如果您无法利用NHProf,只需使用和不使用ToFutureValue对查询进行基准测试。

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

答案 1 :(得分:2)

即使您没有使用 QueryOver 的常规映射关系,也可以直接在 Linq to NHibernate API 上使用加入。< / p>

var historical = session.Query<Historical>()
    .Where(x => nodeIds.Contains(x.Node.ID));

var ncv = (from h in historical
           join n in session.Query<Node>() on h.NodeID equals n.ID
           select h).ToFutureValue(x => x.Count());

var pcv = (from h in historical
           join p in session.Query<Project>() on h.ProjectID equals p.ID
           select h).ToFutureValue(x => x.Count()); // Future is not required here

var nodeCount = ncv.Value;
var projectCount = pcv.Value;