从switch case添加Linq中的条件值

时间:2016-09-04 17:21:21

标签: c# linq

我有一个选择某个值的查询。代码是:

var query = from c in snd.external_invoices.OrderByDescending(x => x.date)
            join o in snd.invoices on c.idexternal_invoices equals o.id_external_invoice
            select new
                     {
                       c.idexternal_invoices,
                       c.businessname,
                       o.number,
                       c.message,
                       c.price,
                       c.date,
                       c.tipologiaPagamento,
                       c.esitoPagamento,
                       c.iduser
                     };

现在我需要根据c.tipologiaPagamento字段中的值在此查询中选择一些值。特别是,我需要选择c.date等于此开关结果的所有值:

            switch (c.tipologiaPagamento)
            {
                case "1":
                    c.date.AddDays(10);
                    break;
                case "2":
                    c.date.AddDays(10);
                    break;
                case "3":
                    DateTime endOfMonth = new DateTime(c.date.Year,
                                   c.date.Month,
                                   DateTime.DaysInMonth(c.date.Year,
                                                        c.date.Month));
                    c.date = endOfMonth;
                    break;
                case "4":
                    DateTime nextMonth = c.date.AddMonths(1);
                    DateTime endOfNextMonth = new DateTime(c.date.Year,
                                   c.date.Month,
                                   DateTime.DaysInMonth(c.date.Year,
                                                        c.date.Month));
                    c.date = endOfNextMonth;
                    break;
                default:
                    break;
            }

所以我需要在查询中选择c.date等于此开关结果的值。

我该怎么做?

感谢所有

3 个答案:

答案 0 :(得分:0)

试试这个:

var query = from c in snd.external_invoices.OrderByDescending(x => x.date)
                        join o in snd.invoices on c.idexternal_invoices equals o.id_external_invoice
                        where c.date== FindDate(c.tipologiaPagamento, c.date)
                        select new
                        {
                            c.idexternal_invoices,
                            c.businessname,
                            o.number,
                            c.message,
                            c.price,
                            c.date,
                            c.tipologiaPagamento,
                            c.esitoPagamento,
                            c.iduser
                        };



  public DateTime FindDate(string tipologiaPagamento, DateTime date)
        {

            DateTime Result = new DateTime();
            switch (tipologiaPagamento)
            {
                case "1":
                    Result = date.AddDays(10);
                    break;
                case "2":
                    Result = date.AddDays(10);
                    break;
                case "3":
                    DateTime endOfMonth = new DateTime(date.Year,
                                   date.Month,
                                   DateTime.DaysInMonth(date.Year,
                                                        date.Month));
                    date = endOfMonth;
                    break;
                case "4":
                    DateTime nextMonth = date.AddMonths(1);
                    DateTime endOfNextMonth = new DateTime(date.Year,
                                  date.Month,
                                   DateTime.DaysInMonth(date.Year,
                                                       date.Month));
                    date = endOfNextMonth;
                    break;
                default:
                    break;
            }
            return Result;

        }

答案 1 :(得分:0)

我不确定你是否可以通过复杂的计算传递你想轻松进入linq表达式?如果我错了,有人可以解决我的问题。

我可能会做什么,因为您的计算仅取决于您的tipologiaPagamento值,并且仅在您当前结果集中的计算中使用date值,而不是打扰自己在SQL端做这件事。只需获取所有需要的信息,并将switch语句放入foreach循环,以根据需要更新应用程序服务器级别的值。可能比SQL服务器更快地进行计算,SQL服务器不一定针对数据操作进行优化,而是更多地用于数据检索。

答案 2 :(得分:0)

这不是很清楚,但非常有效:

from c in ...
...
select new
  {
   Invoices = c.idexternal_invoices,
   Cdate =   c.tipologiaPagamento == 1? c.date
           : c.tipologiaPagamento == 2? c.date.AddDays(10)
           : c.tipologiaPagamento == 3? new DateTime(2015,9,1)
           :  new DateTime(1970,1,1),
   User = c.iduser
 };

...因为它可以编译成LINQ表达式树的一部分而不是在C#侧执行。