Solved

Additional data contained in result from LINQ query

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

912 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