The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

willemr (1) [Avatar] Offline
#1
Hi guys,

I'm making an application in VS 2008 and I have some problems with the LINQ to SQL.

I have a database and I want to take 1 value from it and display that in a messagebox.

This is my code

DataClasses1DataContext dc = new DataClasses1DataContext();

var RFID = "3973128c";
var query =
(from a in dc.GetTable<TERMINALUSER>()
select a);
var id =
(from c in query
where c.IDENTIFICATIONDATA == RFID
select c.TERMINALUSERID);

MessageBox.Show(id);


But I can't get it to work, I can only show the entire line in a datagrid, but then it stops working.

Can any please help?
fabrice.marguerie (224) [Avatar] Offline
#2
Re: LINQ to SQL question
Hi,

id contains an enumeration and not a single object. id is a query.
If you want to get one object, you can use id.First() or id.FirstOrDefault().

If you want to process all the objects returned by id, you can use foreach:

foreach (var idItem in id)
MessageBox.Show(idItem);