?
Solved

Additional data contained in result from LINQ query

Posted on 2009-07-01
3
Medium Priority
?
288 Views
Last Modified: 2013-11-11
Hi,

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")};
 
		supportSpecialist.Items.Add("All");
		foreach (var specialist in allSpecialists.Distinct())
		{
			supportSpecialist.Items.Add(specialist);
		}
		supportSpecialist.SelectedIndex = 0;
	}
}

Open in new window

0
Comment
Question by:Sharp2b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
naspinski earned 2000 total points
ID: 24753592
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())
	supportSpecialist.Items.Add(specialist.CaseOwner);

Open in new window

0
 

Author Comment

by:Sharp2b
ID: 24753749
And voila...here 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.
0
 

Author Closing Comment

by:Sharp2b
ID: 31598735
Just right - sorry I didn't explain that 'supportSpecialist' is the combobox but you got it anyway
0

Featured Post

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question