Join multiple values of type IQueryable

0

I'm receiving from my View an array of values with the days of the week. Sunday, Monday, etc. I want to compare this array with a string attribute of my database and save it to a variable of type "IQueryable". And go adding the contents of the tables. But C # does not let me add this variable.

public JsonResult GetCatequizandosByDiasDisponiveis(string[] diasPertendidos, string anoCatequese)
{
    var catequizandosPorDiasDisponiveis = (from i in db.Inscricao
                                           join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
                                           join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
                                           where  i.AnoCatequese == anoCatequese
                                           select new {
                                               PessoaID = p.PessoaID,
                                               Nome = p.Nome,
                                               Dispo = i.Dias_Preferencial
                                           });

    foreach (string dia in diasPertendidos ?? Enumerable.Empty<string>())
    {
        // para cada 'dia' ver quais os catequizandos possiveis <--- catequizandosPorDiasDisponiveis
        // depois, juntar tudo e eliminar os repetidos
        catequizandosPorDiasDisponiveis += catequizandosPorDiasDisponiveis.Where(d => d.Dispo.Contains(dia));
    }

    return Json(catequizandosPorDiasDisponiveis, JsonRequestBehavior.AllowGet);
}

My problems are here:

  

catechizandosPorDiasDisponible + =   (d => d.Dispo.Contains (day));

    
asked by anonymous 18.04.2016 / 12:50

1 answer

0

You do not need += , method Where() already returns IQueryable .

catequizandosPorDiasDisponiveis = catequizandosPorDiasDisponiveis.Where(d => d.Dispo.Contains(dia));

In any case, this loop adding a where with contains worries me a bit. maybe you'd better do a join with days out, so you could try something of the genre:

from i in db.Inscricao
join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
join d in diasPertendidos on 1 == 1
where 
    i.AnoCatequese == anoCatequese &&
    i.Dias_Preferencial.All(x => d)
select new {
   PessoaID = p.PessoaID,
   Nome = p.Nome,
   Dispo = i.Dias_Preferencial
}

If you want Dias_Preferencial to have all days passed in diasPertendidos , then use the deny method Except with Any .

from i in db.Inscricao
join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
where 
    i.AnoCatequese == anoCatequese &&
    !diasPertendidos.Except(i.Dias_Preferencial).Any()
select new {
   PessoaID = p.PessoaID,
   Nome = p.Nome,
   Dispo = i.Dias_Preferencial
}
    
18.04.2016 / 13:35