使用 NHibernate 获取两列的计数

Get Count of two columns using NHibernate

我需要使用 NHibernate 查询获取两个字段的计数。给出下面 historicalList 的示例数据,结果应该是 1 Project assign to 2 Nodes。如何使用 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();

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

此外,请将 Where+Any 方法与 Distinct 进行对比,看看 Where+Any 是否更快,否则就使用 Distinct 方法。

你可以直接在Linq to NHibernate API上使用join,即使你没有正则映射关系使用 QueryOver.

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;