如何将sql转换为lambda或linq

t5zmwmid  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(412)

如何将下面的sql转换为lambda或linq?

with cte
as (select * from test1
    union all
    select * from test2)

select * from cte
union all 
select sum(columnA),sum(columnB),sum(columnC) from cte
rbl8hiat

rbl8hiat1#

在林肯 UNION ALL.Concat() ,所以:

var cte = test1.Concat(test2);
var sums = new MyModel
  {
     columnA = cte.Sum(c => c.columnA),
     columnB = cte.Sum(c => c.columnB),
     columnC = cte.Sum(c => c.columnC),
  }

return cte.Concat(IEnumerable.Repeat(sums, 1));

你一定要记住 test1 以及 test2 必须是类型 MyModel 以及 MyModel 仅包含 columnA , columnB 以及 columnC .

k7fdbhmy

k7fdbhmy2#

我把两个表放在一个datagridvie中,但是在datagridview的最后一行,我需要全国两个表的总数,我可以为一个表总计一行,为另一个表总计一行,我也不需要,就像我只能有一行两个表的总数一样。


``` `DataContex db = new DataContex(); var query = ( from v1 in db.View1 where shf.Date >= dpDate.Value && shf.Date <= dpDate1.Value select new { v1.Name, v1.Date, v1.Quality, v1.Rat, v1.Total } ).Concat ( from v2 in db.View2 where f.Date >= dpDate.Value && f.Date <= dpDate1.Value select new { v2.Name, v2.Date, v2.Quality, v2.Rat, v2.Total } ).Concat (from View2 in (from v2 in db.View2 where v2.Date >= dpDate.Value && sh.Date <= dpDate1.Value select new { v2.Name, v2.Date, v2.Quality, v2.Rate, v2.Total }) group v2 by new { v2.NRFA } into g select new { Name = "Total:", Date = dpDate1.Value, Quality = (decimal?)g.Sum(p => p.Quality), Rate = (decimal?)g.Sum(p => p.Rate), Total = (decimal?)g.Sum(p => p.Total) } );` 块引用

相关问题