Solved

Additional data contained in result from LINQ query

Posted on 2009-07-01
3
287 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 500 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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