System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Support for multiple parent calls from child entities for more than 3 levels of selection

Open jp0550 opened this issue 5 years ago • 2 comments

var test = dbContext.RootItems
	.SelectMany(x => x.RootFieldList
		.SelectMany(y => y.RootFieldChildList
			.SelectMany(z => z.RootFieldChildListItems
				.Select(a => new
				{
					a,
					z,
					y,
					x
				}))));


var test2 = dbContext.RootItems
	.SelectMany(@"it.RootFieldList
		.SelectMany(it.RootFieldChildList
			.SelectMany(it.RootFieldChildListItems
				.Select(new
				{
					it as a,
					parent as z,
					parent.parent as y,            
					root as x
				})));
");

I'm unable to reproduce the first linq statement using dynamic linq as parent.parent is not valid. Can this be enhanced to support multiple parent entities for linq queries with more than 3 levels?

jp0550 avatar Mar 18 '20 18:03 jp0550

@jp0550 ; can you provide a full working example please?

StefH avatar Mar 19 '20 07:03 StefH

using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<JoinEntity>
            {
                new JoinEntity(),
                new JoinEntity()
            }.AsQueryable();

            var select1 = list.SelectMany(x => x.MainEntity1.Items.SelectMany(y => y.SubItems.SelectMany(z =>
                z.SubSubItems.Select(a => new
                    {
                        SubSubItem = a,
                        SubItem = z,
                        Item = y,
                        JoinEntity = x
                    }
                ))));

            var select2 =
                list.SelectMany(
                    @"MainEntity1.Items.SelectMany(SubItems.SelectMany(SubSubItems.Select(
                        new { 
                                it as SubSubItem, 
                                parent as SubItem, 
                                parent.parent as Item, 
                                root as JoinEntity 
                         })))");
        }
    }


    public class JoinEntity
    {
        public MainEntity1 MainEntity1 { get; set; } = new MainEntity1();
        public MainEntity2 MainEntity2 { get; set; }
    }

    public class MainEntity1
    {
        public List<Item> Items { get; set; } = new List<Item>();
    }

    public class Item
    {
        public List<SubItem> SubItems { get; set; } = new List<SubItem>();
    }

    public class SubItem
    {
        public List<SubSubItem> SubSubItems { get; set; } = new List<SubSubItem>();
    }

    public class SubSubItem
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
    }

    public class MainEntity2
    {
        public string Field1 { get; set; }
    }
}

jp0550 avatar Mar 19 '20 14:03 jp0550