Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good day all, i have this simple table of session, it has a column Enddate, StartDate

I just wrote this simple code using Linq to select the StartDate on page load
C#
  var acc = sdc.Sessions
                .Where(x=> x.StartDate == DateTime.Now.ToString())
                .Select(x => new { x.StartDate });
            Label1.Text = acc.ToString();

// on preview instead of showing me the date it will show me this
//SELECT [t0].[StartDate] FROM [dbo].[Session] AS [t0] WHERE [t0].[StartDate] = @p0 

//Instead the actual date on the StartDate column... Someone should please help me out thanks
Posted

1 solution

Hi Try like this,,


Where clause returns more than one results, more over its is of type IEnumerable<t> </t>.
You should use First,FirstOrDefault, Single ,SingleOrDefault , or Index . to get single item value from the collection for reading the property.

C#
var acc = sdc.Sessions.FirstOrDefault(x => x.StartDate == DateTime.Now.ToString());
           if( acc != null)
           Label1.Text = acc.StartDate.ToString();
 
Share this answer
 
Comments
BillWoodruff 16-Dec-13 4:47am    
Yes ! +5
Karthik_Mahalingam 16-Dec-13 4:54am    
Thanks Bill :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900