Linq交叉连接查询嵌套List

请帮我解决一个场景,我被困住了。

它是这样的。

  1. 使用PropertyGrid动态创建的表和字段列表(在表内)。

    BindingList table = new BindingList
    (); [Serializable] [TypeConverter(typeof(TableConverter))] public class Table { private string _name = string.Empty; private HeaderCollection _hc = new HeaderCollection(); private BindingList _fc = new BindingList(); public Guid key; public Table() { key = Guid.NewGuid(); } [DisplayName( "Table Fields" ), Editor( typeof( FieldCollectionEditor ), typeof( UITypeEditor ) )] public BindingList 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; } } }
  2. 字段类定义

     [Serializable] public class Fields { private string _name = string.Empty; public Guid Key; private List _value = new List(); [Browsable( false )] public List Value { get { return _value; } set { _value = value; } } public Fields() { Key = Guid.NewGuid(); } [DisplayName( "Field Name" )] public string Name { get { return _name; } set { _name = value; } } [DisplayName( "Map" )] public bool Map { get; set; } } 
  3. 字段类包含用于保存一个或多个值的字符串列表。

  4. 我的问题是:需要交叉连接所有与表中所有字段相关的值,并以表格格式显示数据。 我已经使用了这个查询,但这不起作用,因为它逐个取出值,而是我需要一次性来自所有字段的所有值的coross连接。

     var result = table.SelectMany( tbl => tbl.Fields.SelectMany( f => f.Value.Select(v => new { i = v }))); 

    例如,让我们说:

     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 master = new List(); List child = new List(); List child1 = new List(); List child2 = new List(); 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 }; 

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

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

    • 表有一组字段
    • Field有一组值

    并希望得到一个结果,所有的价值都跨越所有领域。 我将首先在所有字段中获取所有值的集合(使用两个查询来获取两个循环):

     var values = from v in (from f in Table.Fields select f.Values) select v; 

    然后加入:

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

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

     class Program { static void Main(string[] args) { var listofInts = new List>(3); listofInts.Add(new List{1, 2, 3}); listofInts.Add(new List { 4,5,6 }); listofInts.Add(new List { 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> CrossJoinLists(IEnumerable> listofObjects) { var result = from obj in listofObjects.First() select new List {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(obj){ obj2 }; } return result; } } 

    感谢您立即回复我尝试了您的方法,但仍然没有以tabluar格式获取值。 让我再次澄清细节。

    我们说吧

     Field1 has Value11 Field2 has Value21 Field3 has Value31 and Value32 Field4 has Value41, Value42 and Value43 

    所有这些字段都属于单个表。

    现在在Cross加入之后,结果应如下所示。

     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. 

    谢谢

    嗡嗡