将求和的sql查询转换为LINQ

时间:2016-02-24 02:36:48

标签: linq-to-sql entity-framework-6

我从遗留的应用程序中获得了以下SQL代码:

SELECT  [timePeriodLabel]
            ,[tariffCost]
            ,[totalUnits]
            ,[totalMoneyValue]
    into    #allRates
    FROM    [halfHourlyCustomers3NF].[dbo].[halfHourlyBillsTariffTotals]
    where   invoiceNumber = @invoiceNumber

    select  'eveningAndWeekend' as timePeriodLabel,
            tariffcost,
            sum(totalUnits) as totalUnits,
            sum(totalMoneyValue) as totalMoneyValue
    into    #newCollection
    from    #allRates
    where   timePeriodLabel = 'evening' or
            timePeriodLabel = 'weekend'
    group by tariffcost

    insert into #newCollection 
    select  'winterDay' as timePeriodLabel,
            tariffcost,
            sum(totalUnits) as totalUnits,
            sum(totalMoneyValue) as totalMoneyValue
    from    #allRates
    where   timePeriodLabel = 'winterDayBeforePeak' or
            timePeriodLabel = 'winterDayAfterPeak'
    group by tariffcost

    insert into #newCollection 
    select  'night' as timePeriodLabel,
            tariffcost as tariffCost,
            sum(totalUnits) as totalUnits,
            sum(totalMoneyValue) as totalMoneyValue
    from    #allRates
    where   timePeriodLabel = 'nightWeekday' or
            timePeriodLabel = 'morningWeekday' or
            timePeriodLabel = 'nightWeekend' or
            timePeriodLabel = 'morningWeekend'
    group by tariffcost

    insert into #newCollection 
    select  * 
    from    #allRates 
    where   not(timePeriodLabel  = 'winterDayBeforePeak' or
            timePeriodLabel = 'winterDayAfterPeak' or
            timePeriodLabel = 'nightWeekday' or
            timePeriodLabel = 'morningWeekday' or
            timePeriodLabel = 'nightWeekend' or
            timePeriodLabel = 'morningWeekend'or
            timePeriodLabel = 'evening' or
            timePeriodLabel = 'weekend')

    select  * 
    from    #newCollection

这有助于从表中过滤和求和数据,如下所示:

winterDayBeforePeak 12.0000 926.5850    111.1900
winterDayAfterPeak  12.0000 151.7650    18.2100
winterPeak          13.0000 331.5100    43.1000
evening              6.0000 172.9250    10.3800
weekend              6.0000 616.5350    36.9900
nightWeekday         8.0000 1362.5150   109.0000
morningWeekday       8.0000 3627.2750   290.1800
nightWeekend         8.0000 533.8800    42.7100
morningWeekend       8.0000 1439.3100   115.1400

我创建了一个名为TariffTotals的List变量的Entity框架应用程序,我想将查询转换的结果添加到另一个名为SummerTariffTotals的List变量中

我已经在MDSN上查看了这方面的文档,我看到了这篇文章:[EF Summing Link] [1]

看了Stack Overflow上的一些答案后,我尝试了以下内容:

var test = db.TariffCaclulations.GroupBy(row => new { row.TariffCost })
                .Select(g => new HalfHourlyBillTariffCalculation()
                {
                    TimePeriodLabel = "eveningAndWeekend",
                    TariffCost = g.Key.TariffCost,
                    TotalUnits = g.Sum(x => x.TotalUnits),
                    TotalMoneyValue = g.Sum(x => x.TotalMoneyValue)
                })
                .ToList();

一旦我有变量,我就会将它添加到求和表中,但是我收到错误实体或复杂类型'Project.Models.HalfHourlyBillTariffCalculation'不能在LINQ to Entities查询中构造。 “}

关于如何克服这个问题的任何指示?

1 个答案:

答案 0 :(得分:1)

错误告诉您无法创建复杂类型。这意味着您无法从linq查询中创建新的Entity对象。你应该创建POCO对象,一切都应该没问题:

您的POCO课程:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.25;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
@interface LoginVC ()
{
  CGFloat animatedDistance;
   CGRect viewFrameKey;
}

 //In ViewDidLoad
   viewFrameKey=self.view.frame;



- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
    heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
    heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];

[UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrameKey];
[UIView commitAnimations];
}

然后你的cal很容易使用它:

public class HalfHourlyBillTariffCalculationPOCO
{
   public string TimePeriodLabel { get; set; }
   public decimal TariffCost { get; set; }
   public decimal TotalUnits { get; set; }
   public decimal TotalMoneyValue { get; set; }
}