I have a problem trying to get a Distinct List of my Class Objects.
For this particular query, I am only interested in grabbing the DLType, There are 3 types of ‘DLType’ that appear in my List multiple times. I just want to grab those three types from the data and use it for the datasource for a combo box.
Here is My Class :-
public class DistributionList
{
public int DistributionID { get; set; }
public string DistributionName { get; set; }
public string DLType { get; set; }
}
Here is where I grab the data:-
public List<DistributionList> GetDistributionLists()
{
DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand("Select * FROM [DistributionLists]",
Connection);
Adapter.SelectCommand = cmd;
Connection.Open();
Adapter.SelectCommand.ExecuteNonQuery();
Adapter.Fill(dt);
Connection.Close();
var DistributionLists = (from d in dt.AsEnumerable()
select new DistributionList
{
DistributionID = d.Field<int>("DistributionID"),
DistributionName = d.Field<string>("DistributionName"),
DLType = d.Field<string>("DLType")
}).ToList();
return DistributionLists;
}
Here I pull the data into my Form :-
var distributionData = dc.GetDistributionLists();
Now i need to get a distinct list from the distributionData Object:-
var query = (from d in distributionData
select new DistributionList
{
DLType = d.DLType
}).Distinct().ToList();
But this doesn’t work, Is there a way of doing this?
The problem with your Linq is that it generates a new
DistributionListfor each value – and because they’re all different instances,Distinctreturns them all. Try reordering the way you process the query, something like this: