none
How to include & filter Children in Lambda expression RRS feed

  • Question

  •    public static MyEntity GetData(this IQueryable<MyEntity> source, int id, int year)
            {
    
                var result = source                
                    .Include(x => x.ChildrenCollecton)
                    .Where(x => x.ID == id)
                    .Where(x => x.ChildrenCollecton.SelectMany(y => y.Year == year)) // this is not working. How do i filter childrens  by year              
                    .FirstOrDefault();           
    
                return result;
            

    Wednesday, May 27, 2015 7:43 PM

Answers

  • Hello lax4u,

    >>// this is not working. How do i filter childrens  by year  

    The SelectMany is used to choice returned columns, for a filter in method-based linq query, we use .Where() method, here I made a simple, you could check it:

    var result = db.Order.Include(o => o.OrderDetailSet)
    
                        .Where(o => o.OrderID == 1)
    
                        .Select(o => new
    
                        {
    
                            OrderID = o.OrderID,
    
                            ODs = o.OrderDetailSet.Where(od => od.ODID == 1)
    
                        });
    

    The result would be an anonymous type, you could create a DTS type as your returned type.

    If this does not work, please feel free to let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 28, 2015 6:25 AM
    Moderator

All replies

  • Hi,

    can you tell us how is "MyEntity" look like in order to understand the structure

    Thursday, May 28, 2015 5:47 AM
  • Hello lax4u,

    >>// this is not working. How do i filter childrens  by year  

    The SelectMany is used to choice returned columns, for a filter in method-based linq query, we use .Where() method, here I made a simple, you could check it:

    var result = db.Order.Include(o => o.OrderDetailSet)
    
                        .Where(o => o.OrderID == 1)
    
                        .Select(o => new
    
                        {
    
                            OrderID = o.OrderID,
    
                            ODs = o.OrderDetailSet.Where(od => od.ODID == 1)
    
                        });
    

    The result would be an anonymous type, you could create a DTS type as your returned type.

    If this does not work, please feel free to let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 28, 2015 6:25 AM
    Moderator