How to make a lambda in?

Asked

Viewed 533 times

1

I have a function convertStringListaInt parameter String.

Example :

String: 1,2.

I’m converting this string into a List of the kind convertStringListaInt_Result owned by number(whole).

How can I make a in using Lambda?

I realized the Entity accepts only primitive types in comparison and that is why the error is generated.

Code:

public static bool ExisteAIPR(string IDEscopo) {
       using(entidadesIUS entidades = new entidadesIUS()) {
       List <convertStringListaInt_Result> resultado = entidades.convertStringListaInt(IDEscopo).ToList();
       return entidades.Escopos.Where(e => resultado.Select(x => x.number).Contains(e.IDEscopo) && e.AIPR == "S").FirstOrDefault() != null ? true : false;
   }
}

Error Generated

An Exception of type 'System.Notsupportedexception' occurred in Entityframework.SqlServer.dll but was not handled in user code

Additional information: Unable to create a Constant value of type 'Model.DTO.convertStringListaInt_Result'. Only Primitive types or enumeration types are supported in this context.

1 answer

2


From what you’re asking, I’m guessing that convertStringListaInt_Result should be something like this:

public class convertStringListaInt_Result 
{
    ...
    public int number { get; set; }
    ...
}

The logic is almost right, only using .Select(x => x.number) you are returning an integer, and the idea is to return a list of integers. Contains is operated on top of a list, not on top of each element.

For this case, you would have to use ToList, and not just Select. Select returns a generating function of the type of the element placed in the predicate, not the list itself. It would look something like this:

    public static bool ExisteAIPR(string IDEscopo)
    {
        using (entidadesIUS entidades = new entidadesIUS())
        {
            List<int> resultado = entidades.convertStringListaInt(IDEscopo).Select(x=> x.number).ToList(); 
            return entidades.Escopos.Where(e => resultado.Contains(e.IDEscopo) 
&& e.AIPR == "S").FirstOrDefault() != null;
        }
    }

Or, by taking advantage of the first version of the answer, you can modify the method convertStringListaInt to return a list of integers:

 public static bool ExisteAIPR(string IDEscopo)
    {
        using (entidadesIUS entidades = new entidadesIUS())
        {
            List<int> resultado = entidades.convertStringListaInt(IDEscopo).ToList();
            return entidades.Escopos.Where(e => resultado.Contains(e.IDEscopo) && e.AIPR == "S").FirstOrDefault() != null;
        }
    }
  • @Marconi Enter here: http://chat.stackexchange.com/rooms/11910/batteryoverflow

Browser other questions tagged

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