locked
Where condition as predicate RRS feed

  • Question

  • User-1783224164 posted

    For example I have below piece of code:

    public class Type0
    {
    	public string field1 { get; set; }
    	public virtual ICollection<Type1> field2s { get; set; } 
    }
    
    public class Type1
    {
    	public string field3 { get; set; }
    	public virtual ICollection<Type2> field4s { get; set }
    }
    
    public class Type2 
    {
    	public string field5 { get; set; }
    	public string field6 { get; set; }
    	public Type3 field7 { get; set; }
    }
    
    public class Type3
    {
    	public string field8 { get; set; }
    	public string field9 { get; set; }
    }
    
    var selectQuery = dbContext.Set<Type0>()
    		.Where(u => u.field1 == 1).Include(b => b.field2s.Select(fg => fg.field4s.Select(pcm => pcm.Type3))).FirstOrDefault();

    And I want to filter records on field4s which is of Type2 object passing a predicate, how can I achieve this? I have made query but having an error "The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties".  Kindly help to advise. Thanks!

    So I need to insert my where condition here:

    var selectQuery = dbContext.Set<Type0>()
    		.Where(u => u.field1 == 1).Include(b => b.field2s.Select(fg => fg.field4s.Select(pcm => pcm.Type3))).FirstOrDefault();

    Wednesday, August 8, 2018 5:46 PM

Answers

  • User1520731567 posted

    Hi pearljem_jem,

    var selectQuery = dbContext.Set<Type0>()
    		.Where(u => u.field1 == 1).Include(b => b.field2s.Select(fg => fg.field4s.Select(pcm => pcm.Type3))).FirstOrDefault();

    And I want to filter records on field4s which is of Type2 object passing a predicate, how can I achieve this? I have made query but having an error "The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties".  Kindly help to advise.

    You cannot use Include to select data like this. Include is used to load related data.

    You should load your entities using Include then select what you want.

    More details,you could refer to it:

    https://stackoverflow.com/questions/38676029/the-include-path-expression-must-refer-to-a-navigation-property-defined-on-the-t

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2018 2:29 AM

All replies

  • User1520731567 posted

    Hi pearljem_jem,

    var selectQuery = dbContext.Set<Type0>()
    		.Where(u => u.field1 == 1).Include(b => b.field2s.Select(fg => fg.field4s.Select(pcm => pcm.Type3))).FirstOrDefault();

    And I want to filter records on field4s which is of Type2 object passing a predicate, how can I achieve this? I have made query but having an error "The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties".  Kindly help to advise.

    You cannot use Include to select data like this. Include is used to load related data.

    You should load your entities using Include then select what you want.

    More details,you could refer to it:

    https://stackoverflow.com/questions/38676029/the-include-path-expression-must-refer-to-a-navigation-property-defined-on-the-t

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2018 2:29 AM
  • User-1783224164 posted

    thanks Yuki!

    Thursday, August 9, 2018 7:52 AM