public int ManageDuplicatePandS() { int rowFocusindex = -1; lst = new ObservableCollection <DataGridViewModel>(); lst = PQDetailsEntity; var query = lst.GroupBy(x => x.SelectedPSID) .Where(g => g.Count() > 1) .ToList(); if (query.Count > 0 && PQDetailsEntity.Count > 1) { var obj1 = query[0].ElementAt(0); var obj2 = query[0].ElementAt(1); int? qty = 1; decimal?productPrice2 = 0; decimal?discountP2 = 0; qty = query[0].ElementAt(0).PQQty + query[0].ElementAt(1).PQQty; productPrice2 = Convert.ToDecimal(query[0].ElementAt(1).PQPrice); discountP2 = query[0].ElementAt(1).PQDiscount; var index1 = lst.IndexOf(query[0].ElementAt(0)); var index2 = lst.IndexOf(query[0].ElementAt(1)); if (productPrice2 != null) { obj1.PQQty = qty; obj1.PQPrice = Convert.ToString(productPrice2); obj1.PQDiscount = discountP2; PQDetailsEntity[index1] = obj1; var row = new DataGridViewModel(ProductList); //row.PQQty = 1; //row.GSTRate = TaxRate; //row.GSTRateStr = Convert.ToString(TaxRate) + "%"; PQDetailsEntity[index2] = row; rowFocusindex = index2; } //for (int i = 0; i < 2; i++) //{ // var p = PQDetailsEntity.Where(e => e.SelectedPSID == obj2.SelectedPSID ).FirstOrDefault(); // if (p.PQPrice == null) // { // PQDetailsEntity.Remove(p); // } //} OnPropertyChanged("PQDetailsEntity"); } else { int count = PQDetailsEntity.Count(x => x.SelectedPSID == null); if (count == 0) { var row = new DataGridViewModel(ProductList); //row.PQQty = 1; //row.GSTRate = TaxRate; //row.GSTRateStr = Convert.ToString(TaxRate) + "%"; PQDetailsEntity.Add(row); OnPropertyChanged("PQDetailsEntity"); rowFocusindex = -1; } else { var emptyRow = lst.Where(y => y.SelectedPSID == null).FirstOrDefault(); rowFocusindex = PQDetailsEntity.IndexOf(emptyRow); } } return(rowFocusindex); }