However the actual solution is to not do the "new", just do "Select p.CategoryID". You will probably then need to use kategoryid.First() to get the number, as your query will return a collection of numbers as it doesn't know there is only one. There will
be some way of making sure the query only returns the first item if that is what you want, but as I said I'm no vb.net expert.
patron
Member
200 Points
143 Posts
how to show the result in label
Apr 30, 2012 08:56 PM|LINK
Hello guys and gals.
This is the code I am using;
Dim db As New DataClassesDataContext Dim kategoryid = From p In db.Categories Where p.CategoryName = "cables" _ Select New With {p.CategoryID}I only want to show the category id in label. How do I do that?
thanks?
update-------------
After trying firstordefault command for the query result, I was able to get this result
{Categoryid=1}. Can you tell me why the output looks like this? how can I get the number 1 only?
AidyF
Star
9250 Points
1578 Posts
Re: how to show the result in label
Apr 30, 2012 09:48 PM|LINK
I'm not VB.net expert, but your "select new with {p.CategoryID} is returning an anonymous object with a property called CategoryID. So you would do;
Label1.Text = kategoryid.First().CategoryID.ToString()
However the actual solution is to not do the "new", just do "Select p.CategoryID". You will probably then need to use kategoryid.First() to get the number, as your query will return a collection of numbers as it doesn't know there is only one. There will be some way of making sure the query only returns the first item if that is what you want, but as I said I'm no vb.net expert.