Linq交叉连接查询

时间:2010-11-03 06:30:10

标签: c# linq-to-objects

  

可能重复:
  Linq cross join query for nested List

请在我遇到困难的情景中帮助我。

就是这样。

  1. 使用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; }
        }
    }
    
  2. 字段类定义

    [Serializable]
    public class Fields {
        private string _name = string.Empty;
        public Guid Key;
        private List<string> _value = new List<string>();
    
        [Browsable( false )]
        public List<string> 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. 我的问题是:需要交叉连接属于表中所有字段的所有值,并以表格格式显示数据。

    我使用了这个查询

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

    在csv文件中取出结果,上面的查询适用于单个字符串,但对于字符串List,我需要所有字段值的交叉连接。

    例如, 让我们说:

    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>();
    

    以及要取出的Linq查询

    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过程来提供结果,如提供的那样在上面的样本中。

0 个答案:

没有答案
相关问题