C# 嵌套列表的Linq交叉连接查询

C# 嵌套列表的Linq交叉连接查询,c#,linq-to-objects,C#,Linq To Objects,请帮我解决一个我被绊倒的场景 事情是这样的 使用PropertyGrid动态创建的表列表和字段列表(表内) BindingList<Table> table = new BindingList<Table>(); [Serializable] [TypeConverter(typeof(TableConverter))] public class Table { private string _name = string.Empty; private He

请帮我解决一个我被绊倒的场景

事情是这样的

  • 使用PropertyGrid动态创建的表列表和字段列表(表内)

    BindingList<Table> table = new BindingList<Table>();        
    [Serializable]
    [TypeConverter(typeof(TableConverter))]
    public class Table {
    private string _name = string.Empty;
    private HeaderCollection _hc = new HeaderCollection();
    private BindingList<Fields> _fc = new BindingList<Fields>();
    public Guid key;
    public Table() {
        key = Guid.NewGuid();
    }
    
    
    [DisplayName( "Table Fields" ), Editor( typeof( FieldCollectionEditor ),
    typeof( UITypeEditor ) )]
    public BindingList<Fields> Fields {
        get { return _fc; }
        set { _fc = value; }
    }
    [DisplayName( "Table Header" )]
    public HeaderCollection Headers {
        get { return _hc; }
        set { _hc = value; }
    }
    
    
    [DisplayName( "Table Name" )]
    public string Name {
        get { return _name; }
        set { _name = value; }
        }
    }
    
    比如说,, 让我们说:

    F1 has Value11
    F2 has Value21
    F3 has Value31 and Value 32
    F4 has Value41, Value42 and Value43
    
    对于每个表和所有字段的值,结果应采用此格式

    Value11 Value21 Value 31 Value 41
    Value11 Value21 Value 31 Value 42
    Value11 Value21 Value 31 Value 43
    Value11 Value21 Value 32 Value 41
    Value11 Value21 Value 32 Value 42
    Value11 Value21 Value 32 Value 43
    
    让我再详细解释一下。例如,如果我们有

    List<string> master = new List<string>(); 
    List<string> child = new List<string>(); 
    List<string> child1 = new List<string>(); 
    List<string> child2 = new List<string>(); 
    and a Linq query to fetch out
    
    var q = from m in master 
            from c1 in child1 
            from c in child 
            from c2 in child2 
            select new { m, c, c1, c2 };
    
    List master=new List();
    列表子项=新列表();
    List child1=新列表();
    List child2=新列表();
    和一个Linq查询来提取
    var q=主数据中的m
    来自child1中的c1
    从c到child
    从c2到child2
    选择新的{m,c,c1,c2};
    

    我确实需要像这样编写上面的查询来提取字段值,但问题是字段是动态生成的,因此其中的值,因此我需要某种递归方法或linq过程来生成上面示例中提供的结果

    您似乎有一个结构,删除了所有其他细节:

    • 表有一个字段集合
    • 字段具有值的集合
    并希望得到一个结果,即所有值跨所有字段。首先,我将自己获取所有字段中所有值的集合(使用两个查询来获取两个循环):

    然后进行连接:

    var res = from v in values
              from f in Table.Fields
              select new {
                 Field = f,
                 Value = v
              };
    

    谢谢你的及时回复 我尝试了你的方法,但仍然没有得到tabluar格式的值。让我再次澄清细节

    比方说

    Field1 has Value11
    Field2 has Value21
    Field3 has Value31 and Value32
    Field4 has Value41, Value42 and Value43
    
    所有这些字段都属于一个表

    现在交叉连接后,结果应该如下所示

    Value11 Value21 Value31 Value41
    Value11 Value21 Value31 Value42
    Value11 Value21 Value31 Value43
    Value11 Value21 Value32 Value41 
    Value11 Value21 Value32 Value42
    Value11 Value21 Value32 Value43
    
    .......
    ------
    
    etc.  
    
    谢谢


    Buzzy

    这应该包括第一篇文章中的主/子样本:

    class Program
    {
        static void Main(string[] args)
        {
            var listofInts = new List<List<int>>(3);
            listofInts.Add(new List<int>{1, 2, 3});
            listofInts.Add(new List<int> { 4,5,6 });
            listofInts.Add(new List<int> { 7,8,9,10 });
    
            var temp = CrossJoinLists(listofInts);
            foreach (var l in temp)
            {
                foreach (var i in l)
                    Console.Write(i + ",");
                Console.WriteLine();
            }
        }
    
        private static IEnumerable<List<T>> CrossJoinLists<T>(IEnumerable<List<T>> listofObjects)
        {
            var result = from obj in listofObjects.First()
                         select new List<T> {obj};
    
            for (var i = 1; i < listofObjects.Count(); i++)
            {
                var iLocal = i;
                result = from obj  in result
                         from obj2 in listofObjects.ElementAt(iLocal)
                         select new List<T>(obj){ obj2 };
            }
    
            return result;
        }
    }
    
    类程序
    {
    静态void Main(字符串[]参数)
    {
    var LISTOFITS=新列表(3);
    Add(新列表{1,2,3});
    添加(新列表{4,5,6});
    添加(新列表{7,8,9,10});
    var temp=交叉连接列表(listofInts);
    foreach(温度中的变量l)
    {
    foreach(l中的变量i)
    控制台。写(i+“,”);
    Console.WriteLine();
    }
    }
    私有静态IEnumerable CrossJoinList(IEnumerable ListoObjects)
    {
    var result=来自listofObjects.First()中的obj
    选择新列表{obj};
    对于(var i=1;i
    这为我提供了字段x值,而我需要所有字段的值x值(交叉连接)。@Buzzy:1您的答案应该作为对问题的编辑来完成(因为它是扩展/细化问题,而不是答案)。第二:我必须考虑这个问题(我想到了递归),第三:埃里克·利珀特已经解决了这个问题。我想:谢谢理查德,我正在浏览埃里克·利珀特的博客,我会回来的,这并不能解决我的问题。谢谢理查德,我的博客在解决我的问题时对我帮助很大。再次表示非常感谢
    Field1 has Value11
    Field2 has Value21
    Field3 has Value31 and Value32
    Field4 has Value41, Value42 and Value43
    
    Value11 Value21 Value31 Value41
    Value11 Value21 Value31 Value42
    Value11 Value21 Value31 Value43
    Value11 Value21 Value32 Value41 
    Value11 Value21 Value32 Value42
    Value11 Value21 Value32 Value43
    
    .......
    ------
    
    etc.  
    
    class Program
    {
        static void Main(string[] args)
        {
            var listofInts = new List<List<int>>(3);
            listofInts.Add(new List<int>{1, 2, 3});
            listofInts.Add(new List<int> { 4,5,6 });
            listofInts.Add(new List<int> { 7,8,9,10 });
    
            var temp = CrossJoinLists(listofInts);
            foreach (var l in temp)
            {
                foreach (var i in l)
                    Console.Write(i + ",");
                Console.WriteLine();
            }
        }
    
        private static IEnumerable<List<T>> CrossJoinLists<T>(IEnumerable<List<T>> listofObjects)
        {
            var result = from obj in listofObjects.First()
                         select new List<T> {obj};
    
            for (var i = 1; i < listofObjects.Count(); i++)
            {
                var iLocal = i;
                result = from obj  in result
                         from obj2 in listofObjects.ElementAt(iLocal)
                         select new List<T>(obj){ obj2 };
            }
    
            return result;
        }
    }