Entity Framework Left Join 使用时出现的错误

Entity Framework Left Join The error I got while using

我正在编写一个查询,想要获取我的一个列表中的内容和另一个列表中的内容,但我遇到了这个错误。

我的代码在这里,当我输入 join 而不是 left join 时,它工作正常,但我想要的值没有出现,请帮忙:D

 var orgRolOlanDuyurular = _context.DuyuruOrgRol.Include(x=>x.Duyuru).ThenInclude(l => l.KL_DuyuruTur).Where(l => l.Duyuru.AktifMi);
 var tumDuyurular = _context.Duyuru.Include(l => l.KL_DuyuruTur).Where(l => l.AktifMi);
 var c = tumDuyurular.LeftJoin(orgRolOlanDuyurular,
                                tmDuyurular => tmDuyurular.Id,
                                orgOlanDuyurular => orgOlanDuyurular.DuyuruId,
                                (tmDuyurular, orgOlanDuyurular) => new DuyuruPaging{Id = tmDuyurular.Id, BaslangicTarihi = tmDuyurular.BaslangicTarihi, BitisTarihi = tmDuyurular.BitisTarihi, DuyuruTurAd = tmDuyurular.KL_DuyuruTur.Ad, Konu = tmDuyurular.Konu });

左外连接是返回第一个集合的每个元素的连接,无论它在第二个集合中是否有任何相关元素。您可以使用 LINQ 通过对组联接的结果调用 DefaultIfEmpty 方法来执行左外部联接。

没有与 T-SQL 完全相同的语法来使用左连接或右连接。但是,您可以利用类似的方法。

假设您有两个实体。 SalesOrderDetailProduct:

public partial class SalesOrderDetail
{
    public int SalesOrderID { get; set; }
    public short OrderQty { get; set; }
    public int ProductID { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal LineTotal { get; set; }
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(1024);

        sb.AppendLine($"Order ID: {SalesOrderID}");
        sb.Append($"   Product ID: {ProductID}");
        sb.AppendLine($"   Qty: {OrderQty}");
        sb.Append($"   Unit Price: {UnitPrice:c}");
        sb.AppendLine($"   Total: {LineTotal:c}");

        return sb.ToString();
    }
}

产品如下:

public partial class Product
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
    public decimal StandardCost { get; set; }
    public decimal ListPrice { get; set; }
    public string Size { get; set; }

    // Calculated Properties
    public int? NameLength { get; set; }
    public decimal? TotalSales { get; set; }
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(1024);

        sb.Append(Name);
        sb.AppendLine($"  ID: {ProductID}");
        sb.Append($"   Color: {Color}");
        sb.AppendLine($"   Size: {(Size ?? "n/a")}");
        sb.Append($"   Cost: {StandardCost:c}");
        sb.Append($"   Price: {ListPrice:c}");
        if (NameLength.HasValue)
        {
            sb.AppendLine($"   Name Length: {NameLength}");
        }
        if (TotalSales.HasValue)
        {
            sb.AppendLine($"   Total Sales: {TotalSales:c}");
        }
        return sb.ToString();
    }
}

现在使用 DefaultIfEmpty() 和 SelectMany() 在 Products 和 Sales 之间执行左连接,如下所示:

var query = (from prod in Products
         join sale in Sales
         on prod.ProductID equals sale.ProductID
           into sales
         from sale in sales.DefaultIfEmpty()
         select new
         {
             prod.ProductID,
             prod.Name,
             prod.Color,
             prod.StandardCost,
             prod.ListPrice,
             prod.Size,
             sale?.SalesOrderID,
             sale?.OrderQty,
             sale?.UnitPrice,
             sale?.LineTotal
         }).OrderBy(ps => ps.Name);

如果你想使用方法语法,你可以按照下面的代码获得相同的结果:

var query = Products.SelectMany(
          sale =>
          Sales.Where(s => sale.ProductID == s.ProductID).DefaultIfEmpty(),
          (prod, sale) => new
          {
              prod.ProductID,
              prod.Name,
              prod.Color,
              prod.StandardCost,
              prod.ListPrice,
              prod.Size,
              sale?.SalesOrderID,
              sale?.OrderQty,
              sale?.UnitPrice,
              sale?.LineTotal
          }).OrderBy(ps => ps.Name);

现在您可以使用一个简单的 foreach 循环,如下所示:

foreach (var item in query)
{
    count++;
    sb.AppendLine($"Product Name: {item.Name} ({item.ProductID})");
    sb.AppendLine($"  Order ID: {item.SalesOrderID}");
    sb.AppendLine($"  Size: {item.Size}");
    sb.AppendLine($"  Order Qty: {item.OrderQty}");
    sb.AppendLine($"  Total: {item.LineTotal:c}");
}

更多信息,您可以访问https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins