Where with enums issue RRS feed

  • Question

  • Hi,

    I have an EF model class:

    public class X

         public StatusEnum Status { get; set; }


    then, when I'm trying to do:

    query = query.Where(x => x.Status == StatusEnum.Approved);

    This works.

    But when I try to do:

    query = query.Where(x => filter.ColumnStatus.Items.Any(y => (StatusEnum)Enum.Parse(typeof(StatusEnum), y) == x.Status));

    (Items are IEnumerable<string>)

    or even simpler:

    query = query.Where(x => filter.ColumnStatus.Items.Any(y => x.Status == StatusEnum.Approved));

    I'm getting an error:

    "... could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information."

    • Edited by Michal Urban Friday, September 25, 2020 12:30 PM
    Friday, September 25, 2020 12:29 PM

All replies

  • Hi Michal Urban,
    You can try to change to "(StatusEnum)Enum.Parse(typeof(StatusEnum), y)" to 
    Enum.Parse(typeof(StatusEnum), y)
    In general, you can't use x variable in"y => x.Status == StatusEnum.Approved".
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 28, 2020 10:02 AM