将SQL转换为LINQ MVC应用程序的问题

时间:2014-11-26 04:20:41

标签: asp.net-mvc tsql

这是我工作的TSQL。我正在努力将其转换为LINQ并在集合中产生结果。非常感谢提前

select  p.CoreSPID as SPID,
  sum(case when pp.PricingCategory = 'Water' and pp.WholesalePricing=0 then pp.FixedCharge end) RetailWaterFixed,sum(case when pp.PricingCategory = 'Water' and pp.WholesalePricing=0 then pp.VolumetricCharge end) RetailWaterVar
from PremisePricings pp,Premises p
and p.PremiseId = pp.PremiseId 
group by p.CoreSPID

UNION

select  p.CoreSPID as SPID,
  sum(case when pp.PricingCategory = 'Water' and pp.WholesalePricing=0 then pp.FixedCharge end) RetailWaterFixed,sum(case when pp.PricingCategory = 'Water' and pp.WholesalePricing=0 then pp.VolumetricCharge end) RetailWaterVar
from PremiseMeteredPricings pp,Premises p,PremiseMeters pm
and p.PremiseId = pm.PremiseId and pm.PremiseMeterId = pp.PremiseMeterId
group by p.CoreSPID

2 个答案:

答案 0 :(得分:0)

如果您对数据库有任何控制权,最简单的解决方案是使用视图:

  1. 根据您提供的问题
  2. 中的SQL代码创建视图
  3. 将视图导入您的实体模型
  4. 使用新实体模型将元素转换为列表
  5. 您的代码可能如下所示:

    List<AllPricing> list = db.AllPricings.ToList();
    

答案 1 :(得分:0)

       var uniondemo = (from p in dc. PremisePricings
              select new { Filename = p.filename, Size = p.size }).Union(
              from k in dc. PremisePricings2
              select new ({SPID=p.CoreSPID })
             .sum(case when pp.PricingCategory = 'Water' and pp.WholesalePricing=0 then

      pp.FixedCharge end) as RetailWaterFixed,sum(case when pp.PricingCategory = 'Water' and 

      pp.WholesalePricing=0 then pp.VolumetricCharge end) as VolumetricCharge  ;