Optimizing a distinction code List&lt;int&gt; types = new List&lt;int&gt;();
foreach (var d in myTableList)
{
    if (!types.Contains((int)d.item_type))
        types.Add((int)d.item_type);
}


I have a table in db called myTable. There is a column in it called item_type. It is integer.

I want to make a list which would have all item_type which are in myTableList, but there shouldn't be duplicates.

I tried:

types = myTableList.Distinct(x =&gt; x.item_type)


but I want types to be list of integers, not items of type myTable.
Try

types = myTableList
    .Select(x =&gt; (int)x.item_type)
    .Distinct();


You may also want to add .ToList() at the end if you want to match original code