按日期部分

时间:2018-03-05 15:50:55

标签: c# sql-server entity-framework-6

我有这个查询实际上是一个视图,但我想控制WHERE子句,所以我决定通过LINQ和EF6解决它:

SELECT NULLIF(SUM([a].[REQUESTED_QTY]), 0) AS [Transaction],
       NULLIF(SUM([a].[ITEM_TOTAL]), 0) AS [Income]
FROM [dbo].[BILL_INFO_DETAIL] AS [a]
    INNER JOIN [dbo].[SERVICE_INFO] AS [b]
        ON [a].[SERVICE_CODE] = [b].[SERVICE_CODE]
WHERE ([a].[INPUT_STATUS] = '1')
      AND ([b].[SERVICE_CODE] IN ( 1610, 1611, 1612 ))
      AND ([a].[PAY_MODE_ID] IN ( 1, 2 ))
      AND (CONVERT(VARCHAR(2), a.STAMP_DATE, 101) IN ( '10', '11', '12' ))
      AND (CONVERT(VARCHAR(4), a.STAMP_DATE, 102) IN ( '2017' ))
      AND ([b].[FEE] > 1)

经过一段时间的反复试验,我得到了这个转换:

public async Task<ViewGuessOnline> GetGuessOnline(int[] serviceCodes, byte[] payModes, string[] months, string year)
{
    try
    {
        using (var context = new FinanceConnection())
        {
            var resultTemp = from a in
                    (from a in context.BILL_INFO_DETAILS
                        where
                            a.INPUT_STATUS == true &&
                            serviceCodes.Contains(a.SERVICE_INFO.SERVICE_CODE) &&
                            payModes.Contains(a.PAY_MODE_ID) &&
                            months.Contains(SqlFunctions.StringConvert(a.STAMP_DATE)) &&
                            (new[] {"2017"}).Contains(SqlFunctions.StringConvert(a.STAMP_DATE)) &&
                            a.SERVICE_INFO.FEE > 1
                        select new
                        {
                            a.REQUESTED_QTY,
                            a.ITEM_TOTAL,
                            Dummy = "x"
                        })
                group a by new {a.Dummy}
                into g
                select new
                {
                    Transaction = g.Sum(p => p.REQUESTED_QTY) == 0 ? (int?) null : (int) g.Sum(p => p.REQUESTED_QTY),
                    Income = g.Sum(p => p.ITEM_TOTAL) == 0 ? (decimal?) null : (decimal) g.Sum(p => p.ITEM_TOTAL)
                };

            // result = (await result.OrderByDescending(o => o.CustomerCode).ToListAsync()).AsQueryable();
        }

        Logger.Info($"{LayerName} -> {callerInfo.MethodName} -> Returning");
        return result;
    }
    catch (Exception exp)
    {
        Logger.Error($"{LayerName} -> {callerInfo.MethodName} -> Exception [{exp.Message}]", exp);
        throw;
    }
}

我对日期部分有疑问。在原始SQL中,2107的第4季度的比较非常简单。但我无法找到适当的内联linq转换,转换为正确的SQL。

此外,即使原始SQL中没有组,我也必须使用虚拟分组。

1 个答案:

答案 0 :(得分:0)

更多的围墙,我能够做到这一点:

from a in
    (from a in db.BILL_INFO_DETAIL
        where
            a.INPUT_STATUS == true &&
            (new int[] {1610, 1611, 1612 }).Contains(a.SERVICE_INFO.SERVICE_CODE) &&
            (new int[] {1, 2 }).Contains(a.PAY_MODE_ID) &&
            a.STAMP_DATE != null &&
            (new int[] {10, 11, 12 }).Contains(a.STAMP_DATE.Value.Month) &&
            a.STAMP_DATE.Value.Year == 2017 &&
            a.SERVICE_INFO.FEE > 1
        select new
        {
            a.REQUESTED_QTY,
            a.ITEM_TOTAL,
            Dummy = "x"
        })
group a by new {a.Dummy}
into g
select new
{
    Transaction = g.Sum(p => p.REQUESTED_QTY) == 0 ? (int?) null : (int) g.Sum(p => p.REQUESTED_QTY),
    Income = g.Sum(p => p.ITEM_TOTAL) == 0 ? (decimal?) null : (decimal) g.Sum(p => p.ITEM_TOTAL)
}

我相应地更改了方法的参数数据类型,现在可以正常工作。