Cannot compare elements of type 'System.Collections.Generic.ICollection`1 Only primitive types, enumeration types and entity types are supported

前端 未结 6 1761
别跟我提以往
别跟我提以往 2020-12-15 16:29

I have written this code

IQueryable sites = context.MainTable.Include(\"RelatedTable\");

if (!string.IsNullOrEmpty(param1)) {
    sites = sites.         


        
6条回答
  •  眼角桃花
    2020-12-15 16:30

    Collection field can be null in this case you get exception NullReferenceException

    when use RelatedTables.Any()

    If you add RelatedTables != null as in a question then you can get

    Cannot compare elements of type 'System.Collections.Generic.ICollection`1[[Project1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'. Only primitive types, enumeration types and entity types are supported.

    If you get the NullReferenceException exception, lazy loading is not turned off and you are good with lazy loading for the field then to prevent exception mark field with virtual keyword to allow lazy loading for the field

    virtual ICollection RelatedTables{ get; set; }
    

    提交回复
    热议问题