Results 1 to 7 of 7

Thread: Entit Framework : Filter child entities

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    48

    Entit Framework : Filter child entities

    Hello !

    I'm using VB.net and entity Framework.
    I have situation where depending on several conditions I construct a query step by step .

    This is the code (Article has 2 relationship with F1 and F2)

    Code:
    Dim list1 As IEnumerable(Of Article)
             list1 = From t In context.Articles.Include("F1").Include("F2") Order By t.name Ascending
                         Select t
             If condition1 then
                 list1 = list1.Where(Function(t2) t2.tp = 1)
             End If
             If condition2 then
                 list1 = list1.Where(Function(t2) t2.id = 7)
             End If
             Myreport.datasource=list1.ToList()
    This code is working , but now I want to add a new condition , and if this is true the child "F1" should be filtered.

    How can I filter the Child "F1"

    Thank you !

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: Entit Framework : Filter child entities

    As far as I'm aware, you can't do so that way. Your query is for Article entities so that's all you can filter on. You can filter the list of Articles by some property or properties of their children but when it comes to including children, it's all or nothing. I think that you'd have to do a separate query for the children.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    48

    Re: Entit Framework : Filter child entities

    Hello 1
    I found this link :
    http://www.leftycoder.com/entity-fra...y-collections/

    But I don't know how to adapt in my case described above , because I create the query step by step and at the end I call the tolist method , and I may have this situation :

    .......
    If condition4 then " Filter the F1"
    If Condition5 then " Filter again the F1"
    If condition6 then "Filter the F2"
    ...
    Myreport.datasource=list1.ToList()


    Have you an idea how can I adapt ?
    Thank you !

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    48

    Re: Entit Framework : Filter child entities

    I try to use this code , but doesn't work :

    Dim list1 As IEnumerable(Of Article)
    list1 = From t In context.Articles Order By t.name Ascending
    Select t
    If condition1 then
    list1 = list1.Where(Function(t2) t2.tp = 1)
    End If
    If condition2 then
    list1 = list1.Where(Function(t2) t2.id = 7)
    End If

    If condition4 then

    Dim a as Article

    For each a in list1
    context.Entry(a).Collection(Function(t2) t2.F1s).Query.Where(Function(t3) t3.quantity = 1).Load()
    Next

    end if
    Myreport.datasource=list1.ToList()

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Entit Framework : Filter child entities

    Try this:

    Code:
    Dim filter As Integer = If(condition1, 1, If(condition2, 7, 0))
    Dim list1 As IEnumerable(Of Article)
    list1 = From t In context.Articles _
            Where t.tp = filter _
            Order By t.name Ascending _
            Select t

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    48

    Re: Entit Framework : Filter child entities

    What are you talking about ?
    do you understand what's my problem ?
    The problem is the code under Condition4

    Code:
    If condition4 then
    
     Dim a as Article
    
     For each a in list1
     context.Entry(a).Collection(Function(t2) t2.F1s).Query.Where(Function(t3) t3.quantity = 1).Load()
     Next
    
     end if

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Entit Framework : Filter child entities

    my mistake then...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width