-1

给定这张表

TABLE A(
IdA NUMERIC PRIMARY KEY
,DescA VARCHAR2(200)
)

TABLE B(
IdB NUMERIC PRIMARY KEY
,IdA NUMERIC
,DescB VARCHAR2(200)
)

我要选择

IdA
, DescA
, Count Of B For Each A

进入自定义 DTO

is dto 的投影不是问题,但我正在寻找一种将计数添加到我的查询的方法

A AAlias = null;
            DtoType myDTO = null;

            return _session.QueryOver<A>(() => AAlias )
          .Select(Projections.Property(() => AAlias.IdA).WithAlias(() => myDTO.IdA),
                  Projections.Property(() => corsoAlias.DescA).WithAlias(() => myDTO.DescA))
            .TransformUsing(Transformers.AliasToBean<DtoType>())
            .List<DtoType>()
            .AsEnumerable();

编辑:域对象:

public class TableA
{
    public Guid Id { get; set; }
    public string Desc { get; set; }
    public ICollection<B> Bs { get; set; }
}

public class TableB
{
    public Guid Id { get; set; }
    public string Desc { get; set; }
}
4

1 回答 1

1

正如您所说,您想为每个 TableA 计算所有 TableB。为此,您将需要一个子查询。

public class TableA
{
    public Guid Id { get; set; }

    public string Desc { get; set; }
}

public class TableB
{
    public Guid Id { get; set; }

    public TableA TableA { get; set; }

    public string Desc { get; set; }
}

public class Dto
{
    public Guid IdA { get; set; }

    public string DescA { get; set; }

    public int CountOfBForEachA { get; set; }
}


public class MyDtoService : IMyDtoService
{
    private readonly ISession _session;

    public MyDtoService(ISession session)
    {
        _session = session;
    }

    public void GetDto()
    {
        TableA tableA = null;
        TableB tableB = null;
        Dto dto = null;

        var countOfBForEachASubQuery = QueryOver.Of(() => tableB)
            .Where(() => tableB.TableA.Id == tableA.Id)
            .Select(x => tableB.Id);

        var result = _session.QueryOver(() => tableA)
            .Select(Projections.Property(() => tableA.Id),
                Projections.Property(() => tableA.Desc).WithAlias(() => dto.DescA),
                Projections.SubQuery(countOfBForEachASubQuery.DetachedCriteria).WithAlias(() => dto.CountOfBForEachA))
            .TransformUsing(Transformers.AliasToBean<Dto>())
            .List<Dto>();
    }
}
于 2021-08-26T08:48:48.077 回答