LINQ从外部列表和内部列表的元素创建新列表

LINQ从外部列表和内部列表的元素创建新列表,linq,Linq,请参阅所需的粗略代码片段。类a是外部类,它有三个类型为B的内部列表 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 strin

请参阅所需的粗略代码片段。类a是外部类,它有三个类型为B的内部列表

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的元素组成,最终输出是分组的。

编辑答案: 对于平坦的目标类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列表时,direction现在为false


这里有一篇文章解释了更多

你能提供你的工作代码吗?你真的需要你的var作为对象名吗?它就像一个算法需要什么。正如我所提到的,这只是一个粗略的代码段,我想@huMptyduMpty是在问这个代码段,因为var是一个保留关键字,所以这个代码段无法编译。根据提供的其他信息更新了答案。非常感谢。这正是需要的。还感谢SelectMany上提供的链接。不用担心-也刚刚意识到还有更多简洁的编写方法,无需重复相同的功能3次-如上面的编辑所示!其中一个方向变量为假。请告知所需的修改。根据要求在上面进行了更改