none
Problem using a List of ParentChild records in a LINQ statement RRS feed

  • Question

  • Let's say i have the following List:
    List<ParentChild> parentChild = new List<ParentChild>();
    parentChild .Add(new ParentChild() {Parent = 1, Children = new List<int>() {3, 4}});
    parentChild .Add(new ParentChild() {Parent = 2, Children = new List<int>() {5}});
    

    and the following tables:

    Table_Data: Id, Description

    Table_Parent: Id, Data_Id, Description

    Table_Children: Id, Parent_Id, Description

    Data_Id in Table_Parent is a 1..n with Id in Table_Data.

    Parent_Id in Table_Children is a 1..n with Id in Table_Parent.

    Now I want to create a LINQ statement that returns a list of Table_Data records that have a Parent equal to one in the parentChild list, and only if one of the Children of that record in the parentChild list is in relation to that Table_Data record.

    Hope it makes sence.

    thanks in advance,

    Erwin


    Erwin Bastiaensen (sharepointology.com) Dont forget to click "Mark as Answer" on the post that helped you.
    Monday, December 5, 2011 7:39 AM

Answers

All replies

  • Hi Erwin,

    If you're using EF, there are navigations between the related entities, you can use Include method(http://msdn.microsoft.com/en-us/library/bb896272.aspx) to retrieve the whole graph records by one query. You can also use Join keyword() to Join three tables on the foreign key.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 6, 2011 7:20 AM
    Moderator
  • Thanks Alan,

    I know about Include and Join, but what I don't know is how to use List to filter on. I don't want to use foreach loops because I want to return an IQueryable.

    kind regards,


    Erwin Bastiaensen (sharepointology.com) Dont forget to click "Mark as Answer" on the post that helped you.
    Tuesday, December 6, 2011 10:29 AM
  • Hi Erw1n,

    I'm not sure if it's what you want:

    using (var db= new EFTestEntities())
                {
                    var test = from d in db.Table_Data
                               select
                                   new
                                   {
                                       DataId = d.Id,
                                       ParentData = from p in d.Table_Parent
                                                    select
                                                        new
                                                        {
                                                            Parent = p.Id,
                                                            Children = p.Table_Children
                                                        }
                                   };
                }
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 13, 2011 8:30 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 16, 2011 6:24 AM
    Moderator
  • Hi,

    I've used the PredicateBuilder (http://www.albahari.com/nutshell/predicatebuilder.aspx), that way I can ForEach the parentChild objects and "Or" them all together in one Linq statement.

    Kind regards,

    Erwin


    Erwin Bastiaensen (sharepointology.com) Dont forget to click "Mark as Answer" on the post that helped you.
    Friday, December 16, 2011 8:18 AM