Join several values of type Iqueryable

Asked

Viewed 332 times

0

I am receiving an array of values from my View with the days of the week. Sunday, Monday, etc. I want to compare this array with a string type attribute of my database and store it in a "Iqueryable" type variable. And add up the contents of the tables. But C# won’t let me add that 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:

CatechetiesPortsDailable += Where(d => d.Contains(day));

1 answer

0

you don’t need the +=, the method Where() already returns a IQueryable.

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

in any case, this loop adding a Where with contains worries me a little. maybe you’d better make a day Join:

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 to Dias_Preferencial possess all days spent in diasPertendidos, then use the negation of the 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
}
  • O had already made a Join to my array, but so returns all the people who had chosen "Sunday AND Monday" and not "Sunday OR Monday". So I was trying to make a counter inside my foreach I don’t know if I’m being explicit.

  • @Simãolopes make a group by the day.

  • Inside the foreach ?

  • There is no way to increment variables of the type Iqueryable?

  • Simon, from what I understand, in your Entity Inscrito, you have a property Dias_Preferencial which is a List<String> with the dates, like ["Domingo", "Segunda", "Terça"] and the array diasPertendidos is a similar list... you want Inscrito.Dias_Preferencial has all the values of diasPertendidos or at least one?

  • At least one value.

  • I did "string convertestringdias = string. Join(",", dayPertendidos);" , "! dayPertendidos.Except(i.Dias_preferential). Any()", and does not return data

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.