使用LINQ选择Into Multiple Object

时间:2018-05-16 10:42:35

标签: c# linq

我想知道,如何使用LINQ实现这一点:

对象:

 public class ObjView
    {
      public A aObj { get; set; }
      Public B bObj { get; set; }
    }

查询:

var query = from a in A
            join b in B on a.B_Id equals b.B_Id 
            select new ObjView(){
            aObject.Item1 = a.Item1,
            aObject.Item2 = a.Item2,
            bObject.Item1 = b.Item1,
            bObject.Item2 = b.Item2,
            };

1 个答案:

答案 0 :(得分:1)

这是你想要达到的目标吗?

class Program
{
    static void Main(string[] args)
    {
        List<ObjA> objAs = new List<ObjA>();
        List<ObjB> objBs = new List<ObjB>();

        var query = (from a in objAs
                     join b in objBs on a.ID equals b.ID
                     select new ObjView()
                     {
                         ObjA = new ObjA()
                         {
                             Item1 = a.Item1,
                             Item2 = a.Item2
                         },
                         ObjB = new ObjB()
                         {
                             Item1 = b.Item1,
                             Item2 = b.Item2
                         }
                     });
    }
}

public class ObjA
{
    public int ID { get; set; }
    public Object Item1 { get; set; }
    public Object Item2 { get; set; }
}

public class ObjB
{
    public int ID { get; set; }
    public Object Item1 { get; set; }
    public Object Item2 { get; set; }
}

public class ObjView
{
    public ObjA ObjA { get; set; }
    public ObjB ObjB { get; set; }
}