Solved

Additional data contained in result from LINQ query

Posted on 2009-07-01
3
285 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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