2013-12-16 201 views
0

请参阅什么是required.class一个粗略的代码片段是具有B型的三个内列出LINQ创建一个从内部和外部列表的元素新的列表

public class A 
{ 
    public List<B> var1; 
    public List<B> var2; 
    public List<B> var3; 
    int x; 
    int y; 
} 

public class B 
{ 
    public string strA; 
    public string strB; 
    public string strC; 
} 

// This is the new class which i want as output  
public class C 
{ 
    public string strC1; 
    public string strC2; 
    public string strC3; 
    public int x; 
    public int y; 
    public bool direction; 
} 

List<A> ListA = SomeClass.GetData(); 
List<B> ListB= new List<B>(); 
List<C> ListC = new List<C>(); 

foreach(A myA in ListA) 
{ 
    ListB = myA.var1; 
    foreach(B mydata1 in ListB) 
    { 
    C var = new C(); 
    var.strC1 = mydata1.strA; 
    var.strC2 = mydata1.strB; 
    var.strC3 = mydata1.strC; 
    var.x = myA.x; 
    var.y = myA.y; 
    var.direction = true; //if input it is true 
    ListC.Add(var); 

    } 

    ListB = myA.var2; 
    foreach(B mydata1 in ListB) 
    { 
    C var = new C(); 
    var.strC1 = mydata1.strA; 
    var.strC2 = mydata1.strB; 
    var.strC3 = mydata1.strC; 
    var.x = myA.x; 
    var.y = myA.y; 
    var.direction = true; //if input it is true 
    ListC.Add(var); 

    } 

    ListC = myA.var3; 
    foreach(B mydata1 in ListB) 
    { 
    C var = new C(); 
    var.strC1 = mydata1.strA; 
    var.strC2 = mydata1.strB; 
    var.strC3 = mydata1.strC; 
    var.x = myA.x; 
    var.y = myA.y; 
    var.direction = false; //if input it is true 
    ListC.Add(var); 

    } 
} 

var grpList = ListC.GroupBy(p => p.strC); 

我的输出ListC外部类它由内部类B和外部类A的元素组成,并且最终输出被分组。

+0

你能提供你的工作代码吗? – Grundy

+1

你真的需要你'var'作为对象名吗? –

+0

它就像一个什么是必需的算法。正如我所提到的,这只是一个粗略的代码片段 – user3106666

回答

0

编辑答案: 为扁平的目标C类

public class C 
{ 
    public string strC1; 
    public string strC2; 
    public string strC3; 
    public int x; 
    public int y; 
    public bool direction; 
} 

只需使用扁平的SelectMany方法和工会了几次......

var C1 = A1.SelectMany(p => p.var1, (p,q) => new C(){ 
    strC1 = q.strA, 
    strC2 = q.strB, 
    strC3 = q.strC, 
    x = p.x, 
    y = p.y, 
    direction = true 
    } 
).Union(
    A1.SelectMany(p => p.var2, (p,q) => new C(){ 
    strC1 = q.strA, 
    strC2 = q.strB, 
    strC3 = q.strC, 
    x = p.x, 
    y = p.y, 
    direction = true 
    }) 
).Union(
    A1.SelectMany(p => p.var3, (p,q) => new C(){ 
    strC1 = q.strA, 
    strC2 = q.strB, 
    strC3 = q.strC, 
    x = p.x, 
    y = p.y, 
    direction = false 
    }) 
).ToList(); 

EDIT2:刚刚意识到我反复相同功能3次以上。写这篇的更简洁的方法是:

var fn1 = new Func<A, B, C>((p,q) => new C(){ 
    strC1 = q.strA, 
    strC2 = q.strB, 
    strC3 = q.strC, 
    x = p.x, 
    y = p.y, 
    direction = !p.var3.Contains(q) 
    }); 

var C1 = A1.SelectMany(p => p.var1, fn1 
).Union(
    A1.SelectMany(p => p.var2, fn1) 
).Union(
    A1.SelectMany(p => p.var3, fn1) 
).ToList(); 

EDIT3 - 方向现在是在上面的例子中假从VAR3列表

下面有一篇文章,解释更多的SelectMany有点... http://dotnet.dzone.com/news/selectmany-probably-the-most-p

何时到来
+0

非常感谢。这正是我们所需要的。 – user3106666

+0

也感谢SelectMany提供的链接。 – user3106666

+0

不用担心 - 也只是意识到有一种更简洁的书写方式,而不用重复相同的功能3次 - 如上面的编辑所示! –

相关问题