使用Linq从左表中获取所有记录,并从右表中获取所有不匹配项,并且还需要实体框架

我有这些桌子

使用Linq从左表中获取所有记录,并从右表中获取所有不匹配项,并且还需要实体框架

这是我需要的输出

使用Linq从左表中获取所有记录,并从右表中获取所有不匹配项,并且还需要实体框架

我已完成

使用Linq从左表中获取所有记录,并从右表中获取所有不匹配项,并且还需要实体框架

我从PriListByProduct表中获取所有行,但是在内部foreach中发出了重复行的问题。 帮助我解决问题。

如果有linq查询可以通过单个查询获取记录,那么它也会告诉我。

这是我的代码

PriceListvm o = new PriceListvm();
        List<PriceListByProductListArrayClass>
priceListByProductList = new List<PriceListByProductListArrayClass>
    ();

    // get all products from PriceListByProduct table
    var obj = db.PriceListByProduct.Where(p => p.PriceListId == id).ToList();

    // now get all products from product table
    o.ProductList = db.Products.Where(p => p.ProductIsInventoryTracking == true)
    .Include(i => i.ProductInventory).ToList();

    // copy data from PriceListByProduct table to list
    foreach (var item in obj)
    {
    PriceListByProductListArrayClass priceListByProduct = new PriceListByProductListArrayClass();

    priceListByProduct.PriceListByProductPriceOrignal = item.PriceListByProductPriceOrignal;
    priceListByProduct.PriceListByProductProductId = item.PriceListByProductProductId;
    priceListByProduct.CurrencyId = item.CurrencyId;
    priceListByProduct.PriceListByProductPriceDiscounted = item.PriceListByProductPriceDiscounted;
    priceListByProduct.PriceListByProductPriceDiscountedNullable = item.PriceListByProductPriceDiscounted;
    priceListByProduct.PriceListByProductProductName = item.Products.ProductName;
    priceListByProduct.isAlreadyExist = true;
    priceListByProductList.Add(priceListByProduct);
    }

    // now get all products from products table except already exist
    foreach (var item in o.ProductList)
    {
    PriceListByProductListArrayClass priceListByProduct = new PriceListByProductListArrayClass();

    // if product is already have in PriceListByProduct table than dont make duplicate it
    foreach(var i in obj)
    {
    if (item.ProductId != i.PriceListByProductProductId)  // product not match than add it
    {
    // check product have inventory record or not
    if (item.ProductIsInventoryTracking == true)
    {
    priceListByProduct.PriceListByProductPriceOrignal = item.ProductInventory.FirstOrDefault().ProductCurrentPrice;
    priceListByProduct.PriceListByProductProductId = item.ProductId;
    priceListByProduct.CurrencyId = 0;
    priceListByProduct.PriceListByProductPriceDiscounted = 0;
    priceListByProduct.PriceListByProductPriceDiscountedNullable = null;
    priceListByProduct.PriceListByProductProductName = item.ProductName;
    priceListByProduct.isAlreadyExist = false;
    priceListByProductList.Add(priceListByProduct);
    } // end of inner if
    } // end of outer if
    } // end of inner foreach
    }  // end of outer foreach

    // copy data list to array
    o.PriceListByProductListArray = priceListByProductList.ToArray();

    if (ModelState.IsValid)
    {
    ViewBag.CurrencyId = new SelectList(db.Currency.ToList(),"CurrencyId","CurrencyName",obj.FirstOrDefault().CurrencyId);
    return PartialView("_EachItemLoadPartial",o);
    }

    ViewBag.CurrencyId = new SelectList(db.Currency.ToList(),o);

谢谢

lidwbbg 回答:使用Linq从左表中获取所有记录,并从右表中获取所有不匹配项,并且还需要实体框架

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3128943.html

大家都在问