Solved

Additional data contained in result from LINQ query

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now