本文介绍了如何使用Switch简化条件Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指出我的正确方向,以便使用switch语句简化以下代码?

Somebody that could point me into the right direction in order to simplify the following code using switch statement?

 var indicators = db.Sses
           .GroupBy(x => x.Estado)
           .Select(x => new IndicatorViewModel
           {
               Count = x.Count(),
               IndicatorType = x.Key.ToString(),
               IndicatorClass = 
               EstadoServicio.Nuevo == x.Key ? "bg-red" : 
               (EstadoServicio.Proceso == x.Key ? "bg-yellow" : 
               (EstadoServicio.Aprobación == x.Key ? "bg-aqua" : "bg-green"))
           ,
               IconClass =
                EstadoServicio.Nuevo == x.Key ? "fa-bookmark-o" :
               (EstadoServicio.Proceso == x.Key ? "fa-bell-o" :
               (EstadoServicio.Aprobación == x.Key ? "fa-calendar-o" : "fa-heart-o")),
               Total = x.Count()/total
           });

推荐答案

如果要使用开关盒,则可以执行以下操作:

You can do something like this, if you want to use switch case:

        var indicators = db.Sses
            .GroupBy(x => x.Estado)
            .Select(
                delegate(YOUR_TYPE x)
                {
                    var ivm = new IndicatorViewModel
                    {
                        Count = x.Count(),
                        IndicatorType = x.Key.ToString(),
                        Total = x.Count()/total
                    };

                    switch (x.Key)
                    {
                        case "bg-red":
                            ivm.IndicatorClass = EstadoServicio.Nuevo;
                            //ivm.IonClass = 
                            break;

                        // etc.
                    }

                    return ivm;
                }
            );

这篇关于如何使用Switch简化条件Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:19