在nhibernate中连接两个具有特定列的表

时间:2015-01-24 10:07:41

标签: nhibernate queryover

我有两个表:客户和产品

public class Customers
{
    public virtual int Id { get; set; }
    public virtual string CName { get; set; }
    public virtual int Age { get; set; }
    public virtual string Address { get; set; }
    public virtual int Salary { get; set; }
}
public class Product
{
    public virtual int Id { get; set; }
    public virtual string PName { get; set; }
    public virtual Customers CustomerID { get; set; }
    public virtual int Amount { get; set; }
}

在DB中使用此值:

------------Customer Table----------------
| id  |  CName  | Age | Address | Salary |
------------------------------------------
| 1   |  Ben    | 18  |    a    | 1000   |
| 2   |  Mark   | 20  |    b    | 2000   |
| 3   |  Ben    | 18  |    a    | 3000   |
| 4   |  Ben    | 19  |    c    | 4000   |
| 5   |  Mark   | 20  |    b    | 5000   |
| 6   |  Jane   | 21  |    d    | 6000   |

------------Customer Table----------------
|  id  |  PName  | CustomerID_id | Amount |
------------------------------------------
| 1    |  A      |       1       | 5      |
| 2    |  B      |       2       | 10     |
| 3    |  C      |       1       | 15     |
| 4    |  D      |       2       | 20     |
| 5    |  E      |       2       | 25     |
| 6    |  F      |       6       | 30     |
| 7    |  G      |       6       | 40     |

当我在SQL Server Management中运行此查询时:

SELECT CName , Amount
FROM [TestNhibernate].[dbo].[Product]
 Inner Join [TestNhibernate].[dbo].[Customers]
   on [TestNhibernate].[dbo].[Product].[Customerid_id] 
             = [TestNhibernate].[dbo].[Customers].[id]

SQL结果是:

-------------------
| CName  | Amount |
-------------------
| Ben    | 5      |
| Mark   | 10     |
| Ben    | 15     |
| Mark   | 20     |
| Mark   | 25     |
| Jane   | 30     |
| Jane   | 40     |

当我运行此查询时

SELECT CName , Sum(salary) as SumSalary, sum(amount) as SumAmount
FROM [TestNhibernate].[dbo].[Product]
  Inner Join [TestNhibernate].[dbo].[Customers]
   on [TestNhibernate].[dbo].[Product].[Customerid_id] 
             = [TestNhibernate].[dbo].[Customers].[id] 
Group By Cname

结果是:

----------------------------------
| CName  | SumSalary | SumAmount |
----------------------------------
| Ben    | 2000      |  20       | 
| Jane   | 12000     |  70       |
| Mark   | 6000      |  55       |
----------------------------------

如何在NHiberante查询中表达?

更新:一些尝试

我试试这段代码

session
    .QueryOver<Product>()
    .JoinQueryOver<Customers>(p => p.CustomerID)
    .SelectList(w => w
      .Select(x => x.Amount)
      .Select(z => z.CustomerID))
    .List<object[]>() 

这已经完成,但是当我写这段代码时

session
    .QueryOver<Product>()
    .JoinQueryOver<Customers>(p => p.CustomerID)
    .SelectList(w => w
      .Select(x=>x.Amount)
      .Select(z=>z.CustomerID.CName))
    .List<object[]>() 

不起作用!

1 个答案:

答案 0 :(得分:0)

根据问题中的信息,有一些QueryOver语法草稿,应该有助于理解。首先,我们应该创建一些代表结果的DTO:

public class ProductDTO
{
    public virtual string ClientName { get; set; }
    public virtual decimal SumSalary { get; set; }
    public virtual decimal SumAmount { get; set; }
}

现在,我们应该有更多标准命名的商业模式:

public class Product
{
    ...
    // instead of this
    //public virtual Customers CustomerID { get; set; }
    // we should use 
    public virtual Customers Customers { get; set; }
}

就我个人而言,我宁愿看到 Customer 而不是客户 ...但仍然优于CustomersID

因为我们这里的映射必须是<many-to-one表示引用关系,而不仅仅是<property - 表示值类型/整数。

现在查询:

// to have access to client name
Customers client = null;
// that would be result - used for proper columns aliasing
ProductDTO dto = null;

var result = session.QueryOver<Occupation>()
    .JoinQueryOver<Customers>(p => p.Customers , () => client)
    .SelectList(w => w
        // SUM
        .SelectSum(x => x.Amount)
            .WithAlias(() => dto.SumAmount)
        .SelectSum(x => x.Salary)
            .WithAlias(() => dto.SumSalary)
        // GROUP BY
        .Select(x => client.CName)
            .WithAlias(() => dto.ClientName)
    )
    // we do have enough info to ask NHibernate for 
    // fully typed result - Product DTO
    .TransformUsing(Transformers.AliasToBean<ProductDTO>())
    .List<ProductDTO>();

这应该让我们知道如何使用NHibernate进行查询。 另外,我建议使用IList<Products>

扩展客户