请参阅什么是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的元素组成,并且最终输出被分组。
你能提供你的工作代码吗? – Grundy
你真的需要你'var'作为对象名吗? –
它就像一个什么是必需的算法。正如我所提到的,这只是一个粗略的代码片段 – user3106666