Как избежать Dupliate value в View - PullRequest
0 голосов
/ 24 октября 2019

У меня есть ProductList с продуктом разных категорий. Я не хочу, чтобы мое представление содержало дублирующиеся значения.

screenshot product list

ProductController.cs

public ActionResult ProductList () {

        IEnumerable<ProductListDTO> ProductList;
        if (User.IsInRole("Admin"))
        {
            ProductList = from a in _db.ProductMsts
                              join b in _db.ProductTypeMsts on a.fk_prodtypeid equals b.pk_prodtypeid
                              select new ProductListDTO
                              {
                                  pk_ProductId = a.pk_ProductId,
                                  ProductType = b.Description,
                                  ProductName = a.ProductName,
                                  OriginalPrice = a.OriginalPrice,
                                  ProductQuantity = a.ProductQuantity,
                                  SellingUptoPrice = a.SellingUptoPrice
                              };

        }
        else
        {
             ProductList = from a in _db.ProductMsts
                              join b in _db.ProductTypeMsts on a.fk_prodtypeid equals b.pk_prodtypeid
                           where a.username==User.Identity.Name

                              select new ProductListDTO
                              {
                                  pk_ProductId = a.pk_ProductId,
                                  ProductType = b.Description,
                                  ProductName = a.ProductName,
                                  OriginalPrice = a.OriginalPrice,
                                  ProductQuantity = a.ProductQuantity,
                                  SellingUptoPrice = a.SellingUptoPrice
                              };
        }

        return View(ProductList);

1 Ответ

0 голосов
/ 24 октября 2019

Вы можете использовать distinct() с синтаксисом запроса, который вы использовали.

ProductList = from a in _db.ProductMsts
                              join b in _db.ProductTypeMsts on a.fk_prodtypeid equals b.pk_prodtypeid
                              select new ProductListDTO
                              {
                                  pk_ProductId = a.pk_ProductId,
                                  ProductType = b.Description,
                                  ProductName = a.ProductName,
                                  OriginalPrice = a.OriginalPrice,
                                  ProductQuantity = a.ProductQuantity,
                                  SellingUptoPrice = a.SellingUptoPrice
                              }.Distinct();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...