如何合并两个EF查询?

时间:2012-12-05 18:14:45

标签: .net vb.net entity-framework

我必须查询,我需要将两者合并为一个结果,我已经尝试使用Union和Concat,但没有成功:

     Dim PayPalPurchases = (From p In ctx.PayPalAPI Where p.Users.Email = userEmail Select
                           KeyPay = p.pay_key,
                           PurchaseDate = p.payment_request_date,
                           Result = p.status,
                           item_sku = p.memo,
                           price = p.transaction_amount).ToList

        Dim licenze = (From lic In ctx.Licenses Where lic.Users.Email = userEmail
                           Select KeyPay = lic.Note,
                           PurchaseDate = lic.Date,
                           Result = "Free",
                           item_sku = lic.Sku,
                           price = 0)

1 个答案:

答案 0 :(得分:1)

创建一个每个查询将结果存储到的类:

Public Class ResultData

  Public KeyPay As String
  Public PurchaseDate As String
  Public Result As String
  Public item_sku As String
  Public price As String

End Class

然后创建一个包含两个结果的列表

Dim PayPalPurchases = (From p In ctx.PayPalAPI
                       Where p.Users.Email = userEmail
                       Select New ResultData With
                        {
                          .KeyPay = p.pay_key,
                          .PurchaseDate = p.payment_request_date,
                          .Result = p.status,
                          .item_sku = p.memo,
                          .price = p.transaction_amount
                        }).ToList

Dim licenze = (From lic In ctx.Licenses
               Where lic.Users.Email = userEmail
               Select New ResultData With
                {
                  .KeyPay = lic.Note,
                  .PurchaseDate = lic.Date,
                  .Result = "Free",
                  .item_sku = lic.Sku,
                  .price = 0
                }).toList

Dim combinedList As List(Of ResultData)

combinedList.AddRange(PayPalPurchases)
combinedList.AddRange(licenze)