how to obtain data in a single query based on a list? LINQ

0

I get a list that contains the level of access to a request and I want to get a list of requests that have this level.

when returning "return vrequest.AsEnumerable (). ToList ();" I get this error You can not create a constant value of type 'SistemaControlGastos.ModeloBD.CatNivel'. Only primitive types or enumeration types are supported in this context.

then in the part of where it would have to change to make the condition that meets the list of levels

    public List<object> getRequestGerent()
    {
             List<CatNivel> listNivel = Authweb.listNivel.Where(x => x.tipo == "primary").ToList();
             var vrequest = (from trequest in Context.Request
                        where listNivel.Any(cn => cn.nivel == trequest.position)
                        select new
                        {
                            idRequest = trequest.IdRequest,
                            requestDate = trequest.nRequesition.requestDate,
                            cantidad = (from tpd in Context.Product
                                        where tpd.FKReq == trequest.IdRequest
                                        select tpd.totalMount).ToList().Sum(),
                            status = trequest.nRequesition.status,
                            nivel = trequest.position,
                            cCot = (from tc in Context.Cotizacion
                                    where tc.FKRequest == trequest.IdRequest
                                    select tc).Count(),
                            categoria = trequest.nRequesition.categoria
                        });
        return vrequest.AsEnumerable<object>().ToList();
    }

example

in this is what I want to do but in a single consultation

var rProvidier = Context.regProvider
                                .Where(x => x.FKRequest.Equals(vrequest.IdRequest))
                                .ToList();
        List<CatProvider> lcp = new List<CatProvider>();
        for (int i = 0; i < rProvidier.Count; i++)
        {
           lcp.Add(getProvider(rProvidier[i].FKCatProvider));
        }
    public CatProvider getProvider(int idp)
    {
        var cp = (from tp in Context.CatProvider
                  where tp.IdProvider == idp
                  select tp).FirstOrDefault();
        return cp;
    }
    
asked by Luis Alberto Acosta 14.02.2018 в 15:37
source

2 answers

2

Notice that you have a ToList (). sum () and a count without ToList () I do not remember which of the 2 is wrong but it seems to me that the tolist (). sum ()

should look like this

public List<object> getRequestGerent()
{
   var listNivel = 
         Authweb.listNivel.Where(x => x.tipo == "primary").select(x => x.nivel).ToList();
         var vrequest = (from trequest in Context.Request
         where listNivel.Contains(trequest.position)
                    select new
                    {
                        idRequest = trequest.IdRequest,
                        requestDate = trequest.nRequesition.requestDate,
                        cantidad = (from tpd in Context.Product
                                    where tpd.FKReq == trequest.IdRequest
                                    select tpd.totalMount).Sum(),
                        status = trequest.nRequesition.status,
                        nivel = trequest.position,
                        cCot = (from tc in Context.Cotizacion
                                where tc.FKRequest == trequest.IdRequest
                                select tc).Count(),
                        categoria = trequest.nRequesition.categoria
                    });
    return vrequest.AsEnumerable<object>().ToList();
}
    
answered by 19.02.2018 / 18:49
source
0

Yes, in where you should check that in listNivel there is an element with nivel equal to the value of property position :

List<CatNivel> listNivel = Authweb.listNivel.Where(x => x.tipo == "primary").ToList();
var vrequest = (from trequest in Context.Request
                where listNivel.Any(cn=> cn.nivel==trequest.position)
                select new
                {
                    idRequest = trequest.IdRequest,
                    requestDate = trequest.nRequesition.requestDate,
                    cantidad = (from tpd in Context.Product
                                where tpd.FKReq == trequest.IdRequest
                                select tpd.totalMount).ToList().Sum(),
                    status = trequest.nRequesition.status,
                    nivel = trequest.position,
                    cCot = (from tc in Context.Cotizacion
                            where tc.FKRequest == trequest.IdRequest
                            select tc).Count(),
                    categoria = trequest.nRequesition.categoria
                });
    
answered by 14.02.2018 в 15:53