# BPM - Remove Duplicates in DataTable Variable

**URL:** <https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032>\
**Category:** Epicor ERP 10\
**Tags:** bpm\
**Created:** [April 26, 2021, 8:25pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032 "2021-04-26T20:25:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_Bird](https://www.epiusers.help/user_avatar/www.epiusers.help/rick_bird/32/77664_2.png) [@Rick\_Bird](https://www.epiusers.help/u/Rick_Bird)\
**Post date:** [April 26, 2021, 8:25pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/1 "2021-04-26T20:25:41Z")

</div>

I have a BPM that is getting PartBin records for a given Part Number. However, what I am interested in is the Bin’s, only the distinct Bin’s.  
None of the Query Widgets permit that the Join be specified, so I am thinking I might be able to use a bit of C# code to reassign my Tableset Variable to a Distinct version of it… but I’m obviously missing something.  
I would prefer to ‘filter’ in place, but I’ve even tried to assign it to another Tableset variable of the same type and it says it’s read only, which is confusing since I can update it with Fill/Update Table by Query.

Among other fails, here is what I’ve tried:

`PartBinOnHandDist.PartBinSearch = PartBinOnHand.PartBinSearch.Distinct();`

I might need this to be a GroupBy on BinNum, though since other columns have data (like Qty, UOM, Lot) that would make them distinct.

---

<div class="post-metadata">

**Author:** ![timshuwy](https://www.epiusers.help/user_avatar/www.epiusers.help/timshuwy/32/90122_2.png) [@timshuwy](https://www.epiusers.help/u/timshuwy)\
**Post date:** [April 26, 2021, 8:36pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/2 "2021-04-26T20:36:48Z")

</div>

try doing this?

```auto
var PartBinOnHandDist = PartBinOnHand.Select.Distinct(x=>x.PartBinSearch);

```

or this:

```auto
var myRecords = PartBinOnHand.GroupBy(x=>x.PartBinSearch);

```

or something like this?

---

<div class="post-metadata">

**Author:** ![Rick\_Bird](https://www.epiusers.help/user_avatar/www.epiusers.help/rick_bird/32/77664_2.png) [@Rick\_Bird](https://www.epiusers.help/u/Rick_Bird)\
**Post date:** [April 26, 2021, 9:03pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/3 "2021-04-26T21:03:28Z")

</div>

Perhaps, but neither of those pass syntax check. From what you provided I got to this, but my PartBinOnHandDist appears to be empty when I add it to a Message box.

```
Syntax OK:
var PartBinOnHandDist = PartBinOnHand.PartBinSearch.Distinct().Select(x => x);

Syntax OK:
var PartBinOnHandDist = PartBinOnHand.PartBinSearch.GroupBy(x => x.BinNum);

Syntax OK:
var PartBinOnHandDist = PartBinOnHand.PartBinSearch.GroupBy(x => new {x.BinNum, x.BinDesc});

```

I’m pretty sure I need to Group by BinNum & BinDesc, but all of these do not seem to actually set my PartBinOnHandDist variable I created in the BPM.

---

<div class="post-metadata">

**Author:** ![timshuwy](https://www.epiusers.help/user_avatar/www.epiusers.help/timshuwy/32/90122_2.png) [@timshuwy](https://www.epiusers.help/u/timshuwy)\
**Post date:** [April 26, 2021, 10:29pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/4 "2021-04-26T22:29:14Z")

</div>

> [@Rick\_Bird](#):
>
> `var PartBinOnHandDist = PartBinOnHand.PartBinSearch.GroupBy(x => new {x.BinNum, x.BinDesc});`

Ahh… you need to have something more than… I didn’t know that you wanted the sum of the qty… to do that, you need to select and sum the data. Something like: (untested).

```c
var PartBinOnHandDist = PartBinOnHand.PartBinSearch.GroupBy(x => new {x.BinNum, x.BinDesc}).Select(x=> new{x.BinNum, decimal qty = xsum(y=>.OnHand)});

```

This site has a bunch of examples: [LINQ Sum](https://www.csharp-examples.net/linq-sum/)

---

<div class="post-metadata">

**Author:** ![Rick\_Bird](https://www.epiusers.help/user_avatar/www.epiusers.help/rick_bird/32/77664_2.png) [@Rick\_Bird](https://www.epiusers.help/u/Rick_Bird)\
**Post date:** [April 26, 2021, 10:48pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/5 "2021-04-26T22:48:37Z")

</div>

Actually, I don’t need to sum the qty, I just need a list of the distinct BinNum and their Descriptions.

`var PartBinOnHandDist = PartBinOnHand.PartBinSearch.GroupBy(x => new {x.BinNum, x.BinDesc});`  
yields an empty PartBinOnHanddist TableSet, but there’s no syntax error, not sure what I’m missing.

---

<div class="post-metadata">

**Author:** ![LBARKER](https://www.epiusers.help/user_avatar/www.epiusers.help/lbarker/32/31072_2.png) [@LBARKER](https://www.epiusers.help/u/LBARKER)\
**Post date:** [April 27, 2021, 8:13am UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/6 "2021-04-27T08:13:41Z")

</div>

Try this, it works for me

Select(x =\> new {x.BinNum, x.BinDesc}).Distinct().ToList();

---

<div class="post-metadata">

**Author:** ![Rick\_Bird](https://www.epiusers.help/user_avatar/www.epiusers.help/rick_bird/32/77664_2.png) [@Rick\_Bird](https://www.epiusers.help/u/Rick_Bird)\
**Post date:** [April 27, 2021, 9:38pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/7 "2021-04-27T21:38:32Z")

</div>

> [@LBARKER](#):
>
> Select(x =\> new {x.BinNum, x.BinDesc}).Distinct().ToList();

While the syntax passes, it doesn’t populate the DataTable. Can you provide some context on how you are using this?

Here is my context:  
I have a Tableset Variable called ‘PartBinOnHandDist’ that is of Type ‘PartBinSearchTableset’

After populating the Table with the Fill Table by Query Widget I am using a Custom Code Widget with the following code with the intent to eliminate duplicate rows since the source data has multiple Bins because of Lot Tracking.

`PartBinOnHandDist.PartBinSearch.Select(x => new {x.BinNum, x.BinDesc}).Distinct().ToList();`

---

<div class="post-metadata">

**Author:** ![LBARKER](https://www.epiusers.help/user_avatar/www.epiusers.help/lbarker/32/31072_2.png) [@LBARKER](https://www.epiusers.help/u/LBARKER)\
**Post date:** [April 27, 2021, 10:18pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/8 "2021-04-27T22:18:48Z")

</div>

Ok, mine is solely used in a Custom Code Widget, I’m not prefilling a table  
You may be able to build your query in the Code Widget

```auto
  var InvCusts = Db.InvcHead.Where(i => i.Company == Session.CompanyID && i.Posted == true && i.OpenInvoice == true && i.CreditMemo == false && i.UnappliedCash == false && i.InvoiceBal > 1 && i.DueDate < nextsunday && Db.Customer.Any(c => c.CustNum == i.CustNum && c.NoContact == false && c.SendReminders_c == true)).Select(x => x.CustNum).Distinct().ToList();

```

I got the idea from

> **[C# Remove Duplicates From List - Dot Net Perls](https://www.dotnetperls.com/duplicates)**
>
> Eliminate duplicate elements from a List with Distinct, HashSet, or for-loops in an optimal way.

---

<div class="post-metadata">

**Author:** ![Rick\_Bird](https://www.epiusers.help/user_avatar/www.epiusers.help/rick_bird/32/77664_2.png) [@Rick\_Bird](https://www.epiusers.help/u/Rick_Bird)\
**Post date:** [April 27, 2021, 10:39pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/9 "2021-04-27T22:39:42Z")

</div>

I wasn’t able to figure out how to do this the way I wanted to but I got to the result I wanted. If anyone can provide a way to remove duplicates in a Table, please let me know as I think that would be simpler.  
What I did is I created another Table (same Type) and used the Fill Table by Query to Fill the new Table with the same records except I concatenated the BinNum & Bin Desc into the Bin Desc field with a few horizontal tabs. Then I used an Set Argument/Variable widget to set a String Variable and format the data:  
`string.Join("\n", PartBinOnHandDist.PartBinSearch.AsEnumerable().Select(x => x.BinDesc).Distinct())`

Get’s me a list in a Message Popup, it just doesn’t feel elegant nor efficient.

---

<div class="post-metadata">

**Author:** ![hkeric.wci](https://www.epiusers.help/user_avatar/www.epiusers.help/hkeric.wci/32/52019_2.png) [@hkeric.wci](https://www.epiusers.help/u/hkeric.wci)\
**Post date:** [April 28, 2021, 12:29pm UTC](https://www.epiusers.help/t/bpm-remove-duplicates-in-datatable-variable/78032/10 "2021-04-28T12:29:02Z")

</div>

I usually use GroupBy

```cs
DataTable dtJson = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(callContextBpmData.Character01);
List<UD03> UD03BufferList = new List<UD03>();
 
// We could have the same ECO.PartNum checked out with different Revs
// Group By PartNum, Revision
Dictionary<string, DataRow> dtJsonGroupedByRev =
    (from j in dtJson.AsEnumerable() select j)
        .GroupBy(g => new {
            PartNum = g.Field<string>("PartNum"),
            RevisionNum = g.Field<string>("RevisionNum")
        })
        .ToDictionary(r => string.Concat(r.Key.PartNum, "-", r.Key.RevisionNum),
            r => r.Where(
                x => x.Field<string>("PartNum") == r.Key.PartNum && x.Field<string>("RevisionNum") == r.Key.RevisionNum).First()
            );
 
// Get UD03 Records in Bulk by PartNum, Revision, AltMethod
foreach (var item in dtJsonGroupedByRev)
{
    string sPartNum = item.Value.Field<string>("PartNum");
    string searchString = String.Join("~", new List<string>() {
        item.Value.Field<string>("RevisionNum"),
        item.Value.Field<string>("AltMethod"),
        string.Empty
    });
 
    UD03BufferList.AddRange(
        (from u in Db.UD03
            where
                u.Company == Session.CompanyID
                && u.Key1 == "ENG"
                && u.Key2 == sPartNum
                && u.Key3.StartsWith(searchString)
            select u
        ).ToList()
    );
}

```
