Vb.net 具有多个集合的LINQ嵌套分组

Vb.net 具有多个集合的LINQ嵌套分组,vb.net,linq,left-join,linq-group,Vb.net,Linq,Left Join,Linq Group,我目前正努力在我的对象集合上组装一个LINQ查询:人、车 每个人可以有多辆车 我想选择PERSONS中的所有人员和所有组该人员拥有的所有车辆。到目前为止,我写的问题是: from c in persons,d in cars where c.id = d.ownerID group by c.Firstname into MG = tolist() 但它只返回有车的人。如果一个人没有车,他就不在名单上。我无法用正确的逻辑进行弥补。试试: List<person>

我目前正努力在我的对象集合上组装一个LINQ查询:人、车

每个人可以有多辆车

我想选择PERSONS中的所有人员和所有组该人员拥有的所有车辆。到目前为止,我写的问题是:

from c in persons,d in cars 
    where c.id = d.ownerID 
    group by c.Firstname into MG = tolist() 
但它只返回有车的人。如果一个人没有车,他就不在名单上。我无法用正确的逻辑进行弥补。

试试:

List<person> plist = new List<person>();
        plist.Add(new person(1, "a"));
        plist.Add(new person(2, "b"));
        plist.Add(new person(3, "c"));
        plist.Add(new person(4, "d"));

        List<cars> clist = new List<cars>();
        clist.Add(new cars(1, "c1"));
        clist.Add(new cars(1, "c2"));
        clist.Add(new cars(1, "c5"));
        clist.Add(new cars(2, "c1"));
        clist.Add(new cars(3, "c1"));
        clist.Add(new cars(3, "c5"));
        clist.Add(new cars(3, "c3"));
        clist.Add(new cars(3, "c2"));
        clist.Add(new cars(4, "c2"));
        clist.Add(new cars(4, "c5"));


        var result = from p in plist
                join c in clist on p.id equals c.ownerID into k
                from s in k.DefaultIfEmpty()
                select new { p.firstName , carName = (s == null ? String.Empty :s.name)};

string sss = "";
 foreach (var v in result)
 {
      sss+= ( v.firstName + " : " + v.carName +  " >> "+"\n");
 }
 textBox1.Text = sss;

谢谢你的逻辑。现在更有意义了。
class person
{
    public int id;
    public string firstName;

    public person(int id1, string name)
    {
        id = id1;
        firstName = name;
    }
}

class cars
{
    public int ownerID;
    public string name;

   public cars(int id,string name1)
    {
        ownerID = id;
        name = name1;
    }
}