Data Domain Service上的SUM和GROUP BY lambda表达式

时间:2014-06-26 22:32:00

标签: c# sql lambda edmx domaindatasource

我有一个Silverlight应用程序和一个来自domaindatasource控件的gridview,我使用两个参数从域数据源查询视图,但是从结果中,我需要总计4列,由其他列分组。

My Model View

我的元数据类:

internal sealed class vwAplicacoesMetadata
    {
        // Metadata classes are not meant to be instantiated.
        private vwAplicacoesMetadata()
        {
        }

        public Nullable<int> CodInternoCliente { get; set; }

        public Nullable<int> CodTipoOper { get; set; }

        public string CPFCNPJCliente { get { return this.CPFCNPJCliente; } set { String.Format("{0,-14}", value); } }

        public string TipoClientePFPJ { get; set; }

        public string NomeCliente { get; set; }

        public DateTime DataOper { get; set; }

        public decimal LCQtde { get; set; }

        public decimal LCValor { get; set; }

        public decimal LDQtde { get; set; }

        public decimal LDValor { get; set; }
    }
}

IQueryable函数我需要使用groupby和sum表达式:

public IQueryable<vwAplicacoes> GetVwAplicacoesPorData(DateTime pstrDataInicio, DateTime pstrDataFinal)
    {
        return this.ObjectContext.vwAplicacoes.Where(d => d.DataOper > pstrDataInicio && d.DataOper < pstrDataFinal)
    }

它的工作,现在我需要通过CPFCNPJCliente,NomeCliente,TipoClientePFPJ,CodInternoCliente和CodTipoOper进行分组,并对字段LCValor,LCQtde,LDValor,LDQtde求和。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

return this.ObjectContext.vwAplicacoes
       .Where(d => d.DataOper > pstrDataInicio && d.DataOper < pstrDataFinal)
       .GroupBy(x => new {x.CPFCNPJCliente,x.NomeCliente,x.TipoClientePFPJ,x.CodInternoCliente,x.CodTipoOper})
       .Select(k => new {key = k.Key, 
                         totalLCValor = k.Sum(x=>x.LCValor),
                         totalLCQtde = k.Sum(x=>x.LCQtde),
                         totalLDValor = k.Sum(x=>x.LDValor),
                         totalLDQtde = k.Sum(x=>x.LDQtde)})