LINQ的交叉联接嵌套列表查询 [英] Linq cross join query for nested List

查看:118
本文介绍了LINQ的交叉联接嵌套列表查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请你帮我出的情况下,在那里我得到stucked之一。

Please do help me out in one of the scenario, where I got stucked.

这是这样的。


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

  1. A dynamically created List of table and List of Fields( inside Table ) using 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; }
    }
}


  • Field类定义

  • Field class definition

    [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;
        }
    }
    


  • Field类包含字符串列表举行。一个或多个值

  • Field class contain List of string to hold one or more value.

    我的问题是:需要交叉连接从表beloging到所有领域的所有值和显示在表格格式的数据。
    我已经使用这个查询,但因为它取出来的值,一个接一个,而不是我需要一个coross一气呵成连接来自各个领域的所有值这是行不通的。

    My Problem is : Need to cross join all values beloging to all fields from a table and display the data in tabular format. I have used this query, but this does not work as it fetch out the values one by one, instead i need a coross join of all values from all fields at one go.

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

    例如,
    比方说:

    For Example, Lets say :

    F1 has Value11
    F2 has Value21
    F3 has Value31 and Value 32
    F4 has Value41, Value42 and Value43
    

    结果应该是这种格式为每个表和所有字段的值。

    The result should be in this format for each table and all fields’ value.

    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
    

    让我解释这多一点点。例如,如果我们有

    Let me elaborate this a little bit more. For example if we have

    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 };
    



    我正好需要这样写上面的查询获取了实地值,但问题是字段动态生成的,因此它里面的值,因此,我需要某种recussive方法或LINQ程序到一代产量的结果如在样品上方提供。

    I exactly need to write the above query like this to fetch out field values but the problem is fields are generated dynamically and so the values inside it, hence i need some sort of recussive method or linq procedure to yeild the result as provided in sample above.

    推荐答案

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

    You seem to have a structure, removing all other details:


    • 表有田

    • 的集合
    • 字段有值的集合

    和希望有一个结果,这是所有值交叉的所有字段。我会在自己获得的所有字段的所有值的集合开始(使用两个查询来获得两个循环):

    And want to have a result which is all values cross all fields. I would start with getting a collection of all values across all fields on its own (using two queries to get both loops):

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



    然后做加盟:

    And then do the join:

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

    这篇关于LINQ的交叉联接嵌套列表查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆