Получить все записи из левой таблицы и все несоответствия из правой таблицы с помощью Linq, а также хотите Entity Framework - PullRequest
0 голосов
/ 10 ноября 2019

У меня есть эти таблицы

enter image description here

Вот вывод, который мне нужен enter image description here

Я сделал это

This Output Get

Я получаю все строки из таблицы 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(), "CurrencyId", "CurrencyName", obj.FirstOrDefault().CurrencyId);
    return PartialView("_EachItemLoadPartial", o);

Спасибо

...