Error using Contains RRS feed

  • Question

  • User438705957 posted

    I have two entities in the model, Audit and Audit_Measure. They are in a many to many relationship which is reflected in the model.
    i.e. Audit has a virtual ICollection Audit_Measures, and Audit_Measure has a corresponding virtual ICollection Audits.

    I am trying to return a list of Audit_Measures, for a particular Audit.

    Assuming the usual try..catch..using etc, the code is:

    audit_measures_in_audit = await db.Audit_Measures
                            .Where(am => am.Audits.Contains(audit))                        

    It's falling over on the 'Contains' clause with an exception:
    "Unable to create a constant value of type 'CACS_GNS_Data.Models.Audit'. Only primitive types or enumeration types are supported in this context.."

    I have since managed to get around it, but am not sure why I am getting the error.
    Surely this is why the virtual collections exist in the model.


    Thursday, January 25, 2018 12:19 AM


  • User1400794712 posted

    Hi Madog,

    .Where(am => am.Audits.Contains(audit))                        

    We shouldn't do this operation for ICollection Audits. According to your need, the code should be:

    audit_measures_in_audit = await db.Audit_Measures
                            .Where(am => am.Audits.Any(a=>a.ID==audit.ID)) //ID means the primary key of Audit.

    Best Regards,


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 25, 2018 5:36 AM

All replies