Additional data contained in result from LINQ query


I use the code below to query a DataTable with Linq. The intention is to select all distinct names of the column "Case Owner" and put those in a combo box. Later it should be possible to filter bu these values (data is displayed in a DataGridView).

Basically it works but I wonder why the values I see in the ComboBox are like "{ CaseOwner = John Smith }" and not like the values I see in the original table like "John Smith"?

I guess I could strip off the additional characters but I don't think this should be necessary as I must be doing something wrong here.

If it makes any difference, the data comes from an Excel html file that I read from an OleDbConnection.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
	// Create and open a file open dialog...
	OpenFileDialog ofd = new OpenFileDialog();
	string fileName = string.Empty;
	if (ofd.ShowDialog() == DialogResult.OK)
		fileName = ofd.FileName;
		// Read and display the data...
		DataTable fullTable = GetDataFromExcel(fileName);
		mainDataGridView.DataSource = fullTable;
		var allSpecialists = from name in fullTable.AsEnumerable() select new { CaseOwner = name.Field<string>("Case Owner")};
		foreach (var specialist in allSpecialists.Distinct())
		supportSpecialist.SelectedIndex = 0;

Open in new window

Who is Participating?
naspinskiConnect With a Mentor Commented:
That is because you are returning a IEnumerable of items that have an attribute CaseOwner which is what you really want.

I am not sure where i n your code you are doing this, but if it is the item specialist, simply do this:
foreach (var specialist in allSpecialists.Distinct())

Open in new window

Sharp2bAuthor Commented:
And we go!

Thanks a lot!!!

It works and then I think I also understand more what I see in the debugger as well. I focused entirely on the line:
var allSpecialists = from...
as I was thinking this was the problem.
Sharp2bAuthor Commented:
Just right - sorry I didn't explain that 'supportSpecialist' is the combobox but you got it anyway
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.