0

I hope you can help me with this.

I want to convert this query from SQL in a lambda expression in C#:

select  
    a.Descripcion 
from 
    pb.MantenimientosTipos a
where 
    a.activo = 1 and
    a.idSegmento in (select b.idSegmento 
                     from pb.MaquinasRelSegm b 
                     where b.idMaquina = 67)

How can I do this?

I have two selectList, "a" and "b", the selectList "a" is list from table b filter by a parameter and the selectList "b" is a list from table a filter by SelectList "a"

private SelectList a (bool agregarTodo = false)
{
    var segmentos = pb.MaquinasRelSegm.Where(x => x.idMaquina == MaquinaId).Select(x => x.Segmentos).ToList();

    if (agregarTodo)
    {
        segmentos.Add(new PB.Domain.Entities.Segmentos { idSegmento = 0, Descripcion = "Todos" });
    }

    return new SelectList(segmentos, "idSegmento", "Descripcion");
}       

private SelectList b (byte idSegmento, bool agregarTodo = false)
{
    var tipos = pb.MantenimientosTipos.Where(x => x.idSegmento == idSegmento && x.Activo).ToList();
    if (agregarTodo)
    {
        tipos.Insert(0, new PB.Domain.Entities.MantenimientosTipos { idTipoMTTO = 0, Descripcion = "Todo" });
    }

    return new SelectList(tipos, "idTipoMTTO", "Descripcion")
}

I want to put only one selectList with this SQL query

This is the relationship in SQL SERVER https://drive.google.com/file/d/0BzpCEYwGGpogRGRaOVNXTDBrTWc/view?usp=sharing

4
  • 1
    please, show code you tried to do Commented Sep 10, 2015 at 18:04
  • What are you using to access the database? Commented Sep 10, 2015 at 18:05
  • It would help if you would post a diagram of the tables and their relationships. It is hard to write a query if one doesn't know how the data look like. Commented Sep 10, 2015 at 18:30
  • drive.google.com/file/d/0BzpCEYwGGpogRGRaOVNXTDBrTWc/… this is de ralationship in SQL SERVER Commented Sep 10, 2015 at 18:37

1 Answer 1

1

Thanks for the diagram.

try to use this (assuming that pb is the EF context):

var segmentoIds = pb.MaquinasRelSegm
    .Where(a => a.idMaquina == 67)
    .Select(a => a.idSegmento)
    .ToList();

var description = pb.MantenimientosTipos
    .Where(a => a.Activo && segmentoIds.Contains(a.idSegmento))
    .Select(a => a.Description);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for help me, i was thinking to try LINQ but your answer was great
That is LINQ, just another syntax.
You wanted as a query expression instead (the answer above is called a lambda expression)? var segmentoIds = (from a in pb.MaquinasRelSegm where a.idMaquia == 67 select a.idSegmente).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.