如何对多个表使用左联接linq

时间:2019-03-11 09:40:48

标签: linq asp.net-web-api

我被卡在连接多个表的过程中。

上下文:我有链接到productTable(名称:ORDER_DETAILS)和配方表(ORDER_RECIPEs)的订单表。我可以从产品菜单中订购产品,从配方菜单中订购配方。当我单击购物车btn时,则必须有产品订单和配方订单的完整列表。我得到什么,两个订单都以单个订单合并,但是我希望这些作为单个订单下的2个单独的行。您可以在图片中看到结果。

var data = (from order in orderEntities.ORDERS
                    join customer in orderEntities.CUSTOMERS on order.FK_CustomerEmail_Orders equals customer.CustomerEmail
                    join orderDetail in orderEntities.ORDER_DETAILS on order.OrderId equals orderDetail.FK_OrderId_OrderDetails into s
                    from orderDetail in s.DefaultIfEmpty()

                    join product in orderEntities.PRODUCTS on orderDetail.FK_ProductId_OrderDetails equals product.ProductId into p
                    from product in p.DefaultIfEmpty()
                    join brand in orderEntities.BRANDS on product.FK_BrandId_Products equals brand.BrandId into b
                    from brand in b.DefaultIfEmpty()


                    join orderRecipe in orderEntities.ORDER_RECIPE on order.OrderId equals orderRecipe.FK_OrderId_OrderRecipe into ro
                    from orderRecipe in ro.DefaultIfEmpty()
                    join recipe in orderEntities.RECIPEs on orderRecipe.FK_RecipeId_OrderRecipe equals recipe.RecipeId into r
                    from recipe in r.DefaultIfEmpty()
                    join rBrand in orderEntities.BRANDS on recipe.FK_BrandId_Recipes equals rBrand.BrandId into rb
                    from rBrand in rb.DefaultIfEmpty()

                        //into ps from rev in ps.DefaultIfEmpty()

                    where customer.CustomerEmail == customerEmail && order.OrderStatus == status &&
                    brandId == 960
                        //(brand.BrandId == brandId && rBrand.BrandId == brandId) 

                    orderby order.OrderId descending

选择查询

select new
                    {
                        Brand = new
                        {
                            BrandId         = brand == null ? 0 : brand.BrandId,
                            BrandName       = brand == null ? String.Empty : brand.BrandName,
                            BrandCategory   = brand == null ? String.Empty : brand.BrandCategory
                        },
                        Customer = new
                        {
                            customer.CustomerId,
                            customer.CustomerEmail,
                            customer.CustomerFirstName,
                            customer.CustomerLastName,
                            customer.CustomerMobile,
                            customer.CustomerImageUrl
                        },

                        OrderDetail = new
                        {

                            OrderDetailId           = orderDetail != null ? orderDetail.OrderDetailId : 0 ,
                            OrderDetailQuantity     = orderDetail != null ? orderDetail.OrderDetailQuantity: 0.0 ,
                            OrderDetailTime         = orderDetail != null ? orderDetail.OrderDetailPlaceTime : DateTime.Now,
                            OrderDetailProductId    = orderDetail != null ? orderDetail.FK_ProductId_OrderDetails : 0 ,
                            OrderDetailOrderId      = orderDetail != null ? orderDetail.FK_OrderId_OrderDetails : 0
                        },

                        OrderRecipe = new
                        {
                            OrderRecipeId           = orderRecipe != null ? orderRecipe.OrderRecipeId : 0,
                            orderRecipeQuantity     = orderRecipe != null ? orderRecipe.OrderRecipeQuantity : 0,
                            OrderRecipePlaceTime    = orderRecipe != null ? orderRecipe.OrderRecipePlaceTime : DateTime.Now ,
                            orderRecipeOrderId      = orderRecipe != null ? orderRecipe.FK_OrderId_OrderRecipe: 0,
                            orderRecipeRecipeId     = orderRecipe != null ? orderRecipe.FK_RecipeId_OrderRecipe :0
                        },

                        Product = new
                        {
                            ProductId               = product == null ? 0 : product.ProductId,
                            ProductTitle            = product == null ? String.Empty : product.ProductTitle,
                            ProductOldPrice         = product == null ? 0.0 : product.ProductOldPrice,
                            ProductNewPrice         = product == null ? 0.0 : product.ProductNewPrice,
                            ProductImageUrl         = product == null ? String.Empty : product.ProductImageUrl,
                            ProductContent          = product == null ? String.Empty : product.ProductContent,
                            ProductCategory         = product == null ? String.Empty : product.ProductCategory,
                            ProductSubCategory      = product == null ? String.Empty : product.ProductSubCategory,
                            ProductPostedTime       = product == null ? DateTime.Now : product.ProductPostedTime,
                            ProductStocks           = product == null ? String.Empty : product.ProductStocks,
                            ProductStatus           = product == null ? String.Empty : product.ProductStatus,
                            ProductBrandId          = product == null ? 0 : product.FK_BrandId_Products
                        },


                        Recipe = new
                        {
                            RecipeId                = recipe != null ? recipe.RecipeId: 0 ,
                            RecipeTitle             = recipe != null ? recipe.RecipeTitle : String.Empty,
                            RecipePrice             = recipe != null ? recipe.RecipePrice : 0,
                            RecipeImage             = recipe != null ? recipe.RecipeImage: String.Empty,
                            RecipeCategory          = recipe != null ? recipe.RecipeCategory: String.Empty,
                            RecipePostTime          = recipe != null ? recipe.RecipePostTime : DateTime.Now,
                            RecipeStock             = recipe != null ? recipe.RecipeStock: String.Empty,
                            RecipeStatus            = recipe != null ? recipe.RecipeStatus : false,
                            ProductBrandId          = recipe != null ? recipe.FK_BrandId_Recipes: 0 
                        },


                        order.OrderId,
                        order.OrderPlaceTime,
                        order.OrderCompletedTime,
                        order.OrderStatus,
                        order.FK_CustomerEmail_Orders

                    }).Skip(offset).Take(limit).ToList();

Data JSon

我遵循了这个: Left Join Linq

您可以在此处看到,产品和配方按相同顺序组合,但是如果有产品,则配方应为0,反之亦然。像这样:

order:{
    brand:{ 10 },OrderRecipe:{ 1 },Recipe{1}, orderDetail:{ 0 },products: {0} orderId: 1 ..},{
    brand:{ 10 },OrderRecipe:{ 2 },Recipe{2}, orderDetail:{ 0 },products: {0} orderId: 1 ..},{  
    brand:{ 10 },orderDetail:{ 1 },products: {1},OrderRecipe:{ 0},Recipe{0} orderId: 1...},{
    brand:{ 10 },orderDetail:{ 2 },products: {2},OrderRecipe:{ 0},Recipe{0} orderId: 1...}
  

如果还有其他更好的方法可以做到这一点。请在这里纠正我。

1 个答案:

答案 0 :(得分:0)

您肯定会得到这样的结果,因为您已将两个表与ORDER联接在一起。

您可以做的是: 1)您可以像这样分别制作对象:

var recipe = (from db.order ...
join orderRecipe in orderEntities.ORDER_RECIPE on order.OrderId equals orderRecipe.FK_OrderId_OrderRecipe into ro
                    from orderRecipe in ro.DefaultIfEmpty()
                    join recipe in orderEntities.RECIPEs on orderRecipe.FK_RecipeId_OrderRecipe equals recipe.RecipeId into r
                    from recipe in r.DefaultIfEmpty()
                    join rBrand in orderEntities.BRANDS on recipe.FK_BrandId_Recipes equals rBrand.BrandId into rb
                    from rBrand in rb.DefaultIfEmpty())

然后您可以相应地使用两个对象

2)如果要在同一查询中对两个都使用join。在Visual Studio中对照linq检查生成的sql。您要做的是对产品使用右连接,对配方使用左连接。

我希望这会对您有所帮助。