C#Linq查询转换为一个ObservableCollection命令

时间:2012-04-12 08:45:51

标签: c# vb.net linq

我有一个像这样的结构集合:

Jet     Owner   Item    Comp
AAAA    AAAA    101     2210
AAAA    AAAA    202     2220
AAAA    AAAA    301     5550
AAAA    AAAA    301     5560
AAAA    2210    101     6789
AAAA    2220    202     ABABA
AAAA    5550    301     XXXX0
AAAA    5550    301     XXXX1
AAAA    5560    301     YYYY0
AAAA    5560    301     6612
AAAA    6789    101     ZZZZZ.0
AAAA    6789    101     ZZZZZ.1
AAAA    6789    101     ZZZZZ.2
AAAA    6789    101     ZZZZZ.3
AAAA    ABABA   202     TTTTT.0
AAAA    ABABA   202     TTTTT.1
AAAA    ABABA   202     TTTTT.2
AAAA    6612    301     ZZZZZ.0
AAAA    6612    301     ZZZZZ.1
AAAA    6612    301     ZZZZZ.2
AAAA    6612    301     ZZZZZ.3
AAAB    ...     ...     .....

我需要订购它才能得到这样的结果:

Jet     Owner   Item     Comp
AAAA    AAAA    101      2210
AAAA    2210    101      6789
AAAA    6789    101      ZZZZZ.0
AAAA    6789    101      ZZZZZ.1
AAAA    6789    101      ZZZZZ.2
AAAA    6789    101      ZZZZZ.3
AAAA    AAAA    202      2220
AAAA    2220    202      ABABA
AAAA    ABABA   202      TTTTT.0
AAAA    ABABA   202      TTTTT.1
AAAA    ABABA   202      TTTTT.2
AAAA    AAAA    301      5550
AAAA    5550    301      XXXX0
AAAA    5550    301      XXXX1
AAAA    AAAA    301      5560
AAAA    5560    301      YYYY0
AAAA    5560    301      6612
AAAA    6612    301      ZZZZZ.0
AAAA    6612    301      ZZZZZ.1
AAAA    6612    301      ZZZZZ.2
AAAA    6612    301      ZZZZZ.3
AAAB    ...       ...      .....

我想通过Jet订购,然后在comp中我找到一些在所有者中,在相关组件的主要所有者之后放入行,然后按所有者排序,并按项目的最后一个顺序排序。我用C#linq查询得到了结果,但我需要在VB.NET linq查询中得到相同的结果。 C#Linq查询是:

    var orderedData =(from d in collection
        group d by d.Jet into g
        orderby g.Key
        from d in g
        select new[] {d}.Union(g.Where(c => c.Owner == d.Comp)) into withChildren
        from wc in withChildren
        orderby wc.Item
        select wc).Distinct();

我在VB.NET中试过这个但是没有用:

Dim orderedData = (From d In collection 
              Group d By d.Jet Into Group 
              Order By Group 
              From d In Group 
              Select New withChildren With {d}.Union(gr.Where(Function(c) c.Owner = d.Comp))) 
                          from wc in withChildren
                          Order By wc.pos 
                          Select wc).Distinct
                          from wc in withChildren
                          Order By wc.pos 
                          Select wc).Distinct

1 个答案:

答案 0 :(得分:0)

试一试:

Dim orderedData = (From withChildren In From g In From d In collectionGroup d By d.JetOrder By g.KeyFrom d In gNew () {d}.Union(g.Where(Function(c) c.Owner = d.Comp))From wc In withChildrenOrder By wc.Itemwc).Distinct()