操作方法:使用 Linq to 对象连接三个列表

Don*_*nie 1 linq linq-to-objects c#-4.0

问题是地址没有输出

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace LinqToObjects
          {
            class Program
          {
    static void Main(string[] args)
    {
        var customers = Customer.GetAllCustomers();
        var addresses = Address.GetAllAddresses();
        var addressRelations = AddressRelation.GetAllAddressRelations();

        var results = customers
                    .Join(addressRelations,
                    c => c.CustomerID,
                    ar => ar.CustomerID,
                    (c, ar) => new
                    {
                        CustomerName = c.FirstName + " " + c.LastName,
                        CustomerID = c.CustomerID,
                        AddressRelID = ar.AddressID
                    });
        var resultsJoined = results
                       .GroupJoin(addresses,
                        ar => ar.AddressRelID,
                        a => a.AddressID,
                        (ar, a) => new
                        {
                            CustomerName = ar.CustomerName,
                            AddressLine = addresses.Select(b => b.StreetAddress).FirstOrDefault()
                        });
        foreach(var item in resultsJoined)
        {
            Console.WriteLine(item.CustomerName);
            Console.WriteLine(item.AddressLine);
            Console.WriteLine("-----------------");

        }
    }
}

public class AddressRelation
{
    public int AddressRelationID { get; set; }
    public int CustomerID { get; set; }
    public int AddressID { get; set; }
    public AddressRelation(int id, int customerId, int addressId)
    {
        AddressRelationID = id; CustomerID = customerId; AddressID = addressId;
    }
    public static List<AddressRelation> GetAllAddressRelations()
    {
        var AllAddressRelations = new List<AddressRelation>();//simulate data returned from db
        var addressRelation1 = new AddressRelation(1, 1, 1);
        var addressRelation2 = new AddressRelation(2, 3, 3);
        var addressRelation3 = new AddressRelation(3, 2, 2);
        AllAddressRelations.Add(addressRelation1);
        AllAddressRelations.Add(addressRelation2);
        AllAddressRelations.Add(addressRelation3);

        return AllAddressRelations;
    }
}
public class Address
{
    public int AddressID { get; set; }
    public string StreetAddress { get; set; }
    public Address(int id, string streetAddress)
    {
        AddressID = id; StreetAddress = streetAddress;
    }
    public static List<Address> GetAllAddresses()
    {
        var AllAddresses = new List<Address>();
        Address customer1Address = new Address(1, "Elm St");
        Address customer2Address = new Address(2, "Willow Way");
        Address customer3Address = new Address(3, "Linq Ln");
        AllAddresses.Add(customer1Address);
        AllAddresses.Add(customer2Address);
        AllAddresses.Add(customer3Address);
        return AllAddresses;
    }
}
public class Customer
{
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Customer(int id,string firstName, string lastName)
    {
        CustomerID = id; FirstName = firstName; LastName = lastName;
    }
    public static List<Customer> GetAllCustomers()
    {
        var AllCustomers = new List<Customer>();
        var customer1 = new Customer(1, "James", "T");
        var customer2 = new Customer(2, "Donnie", "H");
        var customer3 = new Customer(3, "Sarah", "H");
        AllCustomers.Add(customer1);
        AllCustomers.Add(customer2);
        AllCustomers.Add(customer3);
        return AllCustomers;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Amy*_*y B 5

该查询不太具有表达性。如果我要使用 LinqToObjects 连接三个列表,我会这样做:

var query =
  from c in customers
  join xr in addressRelations on c.CustomerId equals xr.CustomerId
  join a in addresses on xr.AddressId equals a.AddressId
  select new {Customer = c, Address = a};
Run Code Online (Sandbox Code Playgroud)

看起来又是一个错误。我敢打赌,AddressRelId 是 AddressRelation 表的关键,而不是您想要用来连接到 Address 表的关键。

.GroupJoin(addresses, 
ar => ar.Address**Rel**ID, 
a => a.AddressID,
Run Code Online (Sandbox Code Playgroud)

回复评论:

var query = customers
  .Join(addressRelations,
    c => c.CustomerId,
    xr => xr.CustomerId,
    (c, xr) => new {c, xr})
  .Join(addresses,
    x => x.xr.AddressId,
    a => a.AddressId,
    (x, a) => new {c = x.c, xr = x.xr, a = a})
  .Select(x => new {Customer = x.c, Address = x.a});
Run Code Online (Sandbox Code Playgroud)