Seleccionar caso en LINQ [duplicar]


Esta pregunta ya tiene una respuesta aquí:

¿Cómo puedo traducir esto a LINQ?

select t.age as AgeRange, count(*) as Users
from (
  select case  
    when age between 0 and 9 then ' 0-25'
    when age between 10 and 14 then '26-40'
    when age between 20 and 49 then '60-100'
    else '50+' end as age
  from user) t
group by t.age

¡Gracias!

Author: abatishchev, 2010-11-22

5 answers

Tal vez esto funcione:

from u in users
let range = (u.Age >= 0  && u.Age < 10 ? "0-25" :
             u.Age >= 10 && u.Age < 15 ? "26-40" :
             u.Age >= 15 && u.Age < 50 ? "60-100" :
            "50+")
group u by range into g
select new { g.Key, Count=g.Count() };
 39
Author: Botz3000,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-22 09:57:31

Marque esto puede ayudarle

var query = from grade in sc.StudentGrade
                        join student in sc.Person on grade.Person.PersonID
                                      equals student.PersonID
                        select new
                        {
                            FirstName = student.FirstName,
                            LastName = student.LastName,
                            Grade = grade.Grade.Value >= 4 ? "A" :
                                        grade.Grade.Value >= 3 ? "B" :
                                        grade.Grade.Value >= 2 ? "C" :
                                        grade.Grade.Value != null ? "D" : "-"
                        }; 
 11
Author: Pranay Rana,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-22 10:02:13

Usa algo así:

class AgeHelper
{
    private static Dictionary<IEnumerable<int>, string> dic = new Dictionary<IEnumerable<int>, string>
    {
        { Enumerable.Range(0, 10), "0-25" },
        { Enumerable.Range(10, 5), "26-40" },
        { Enumerable.Range(15, 35), "60-100" }
    };

    public string this[int age]
    {
        get
        {
            return dic.FirstOrDefault(p => p.Key.Contains(age)).Value ?? "50+";
        }
    }
}

El resto de la respuesta de @Botz3000:

from u in users
let range = new AgeHelper()[u.Age]
...
 7
Author: abatishchev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-22 10:16:45

¿Algo así?

var users = (from u in Users
             select new
             {
                User = u,
                AgeRange =
                    u.Age >= 0 && u.Age <= 9 ? "0-25"  :
                    u.Age <= 14              ? "26-50" :
                    u.Age <= 49              ? "60-100":
                                               "50+"
              }).GroupBy(e => e.AgeRange);
 4
Author: Martin Doms,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-22 09:55:39

No conozco ninguna manera de crear SQL eficiente como este, usando una instrucción LINQ. Pero puedes usar:

  1. Utilice un procedimiento almacenado (o función), y llame al procedimiento almacenado desde LINQ.
  2. Usar SQL directo

Seguro que puedes usar muchas sentencias condicionales en línea (? :), pero no creo que el resultado sea eficiente.

 0
Author: GvS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-22 09:48:34