使用LINQ查询将多行转换为具有多列的单行

时间:2017-09-10 08:24:25

标签: c# entity-framework linq

让我们考虑以下两个表:

访客

GuestID    FirstName    Email     
----------------------------------------------
3          Sam          sam.billings@gmail.com

GuestAddress

AddressID    GuestID    AddressType    IsPrimaryAddress   Address1
-------------------------------------------------------------------
3            3          1              True               ABC
4            3          2              False              XYZ

我希望输出像这样:

GuestID    FirstName   Email         IsPrimaryAddress  Address1  IsPrimaryAddress Address1
3          Sam         sam.billings@gmail.com  True    ABC       False             XYZ 

我为SQL Server找到了这种类型的解决方案,但我无法将其转换为linq查询。我尝试了linq查询,但它返回两行。这是否可以使用linq查询?

2 个答案:

答案 0 :(得分:1)

您可以尝试GroupJoin扩展方法并生成以下结果。

var result = db.Guests.GroupJoin(db.GuestAddresses,
                            guest => guest.GuestID,
                            address => address.GuestID,
                            (g, groupedAddress) => new
                            {
                                GuestID = g.GuestID,
                                FirstName = g.FirstName,
                                Email = g.Email,
                                IsPrimaryAddress = groupedAddress.Any(x => x.AddressType == 1) ? groupedAddress.FirstOrDefault(x => x.IsPrimaryAddress).IsPrimaryAddress : false,
                                PrimaryAddress = groupedAddress.Any(x => x.AddressType == 1) ? groupedAddress.FirstOrDefault(x => x.IsPrimaryAddress).Address: string.Empty,
                                IsSecondayAddress = groupedAddress.Any(x => x.AddressType == 2) ? groupedAddress.FirstOrDefault(x => !x.IsPrimaryAddress).IsPrimaryAddress : false,
                                SecondayAddress = groupedAddress.Any(x => x.AddressType == 2) ? groupedAddress.FirstOrDefault(x => !x.IsPrimaryAddress).Address : string.Empty,
                            }).ToList();

答案 1 :(得分:0)

使用多个访客和多个地址更新我已检查过,请尝试以下操作:

 namespace ConsoleApplication1
 {
  class Program
  {
    static void Main(string[] args)
    {
        Guest guest1 = new Guest { Email = "tom@gmail.com", FirstName = "tom", GuestID = 1 };
        Guest guest2 = new Guest { Email = "jerry@gmail.com", FirstName = "jerry", GuestID = 2 };
        List<Guest> listGuests = new List<Guest>();
        listGuests.Add(guest1);
        listGuests.Add(guest2);

        List<Address> AddressList = new List<Address>();
        Address address1 = new Address { AddressID = 1, AddressType = "1", GuestID = 1, IsPrimaryAddress = false, Address1 = "address1" };
        Address address2 = new Address { AddressID = 2, AddressType = "2", GuestID = 1, IsPrimaryAddress = true, Address1 = "address2" };
        Address address3 = new Address { AddressID = 1, AddressType = "1", GuestID = 2, IsPrimaryAddress = true, Address1 = "address3" };
        Address address4 = new Address { AddressID = 1, AddressType = "1", GuestID = 2, IsPrimaryAddress = false, Address1 = "address4" };

        AddressList.Add(address1);
        AddressList.Add(address2);
        AddressList.Add(address3);
        AddressList.Add(address4);

        var queryResult = from primaryAddress in ( AddressList.Where(x => x.IsPrimaryAddress == true))
                          join secAddress in (AddressList.Where(x => x.IsPrimaryAddress == false))
                          on primaryAddress.GuestID equals secAddress.GuestID
                          join guest in listGuests on  secAddress.GuestID equals guest.GuestID
                          select new GuestAddress
                            {
                                FirstName = guest.FirstName,
                                Email = guest.Email,
                                GuestID = primaryAddress.GuestID,
                                IsPrimaryAddress = primaryAddress.IsPrimaryAddress,
                                Address1 = primaryAddress.Address1,
                                SecondayAddress = secAddress.Address1,
                                IsSecondayAddress = secAddress.IsPrimaryAddress
                            };

    }
}
public class Guest
{
    public int GuestID { get; set; }
    public string FirstName { get; set; }
    public string Email { get; set; }
}
public class Address
{
    public int AddressID { get; set; }
    public int GuestID { get; set; }
    public string AddressType { get; set; }
    public bool IsPrimaryAddress { get; set; }
    public string Address1 { get; set; }
}
public class GuestAddress
{
    public string FirstName { get; set; }
    public string Email { get; set; }
    public int GuestID { get; set; }
    public bool IsPrimaryAddress { get; set; }
    public bool IsSecondayAddress { get; set; }
    public string Address1 { get; set; }
    public string SecondayAddress { get; set; }

}

}