Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL C#4.0 query MVC4 , +
I trying to select user record using
 
var customUser = (from u in context.Users
            join pr in context.PasswordRecoveries on u.Id equals pr.userId
            where pr.url == token && pr.requestDateTime.AddHours(12) <DateTime.Now
            select u).First();
 

Whats wrong in this query?
Please Help me.
Posted 11-Oct-13 3:29am
Vi(ky2.5K
Comments
codestar007 at 11-Oct-13 8:30am
   
What is the problem u are facing...??
Simon_Whale at 11-Oct-13 10:10am
   
You need to explain this more as technically there is nothing wrong with the query on its own.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to tell us what errors you get, but EF does not like functions inside it. Do this instead:
 
DateTime dt = DateTime.Now.AddHours(-12);
 
var customUser = (from u in context.Users
            join pr in context.PasswordRecoveries on u.Id equals pr.userId
            where pr.url == token && pr.requestDateTime < dt
            select u).First()
 
Then if it still does not work, tell us what error you get, so we can help you further.
 
You can also use FirstOrDefault() if it won't always find a value. You should also use an order by if you're using 'first' and there could be more than one record, or the one you get will be essentially random.
  Permalink  
Comments
gaswent at 10-Mar-14 3:18am
   
why to choose entity framework when other orms in .net
Christian Graus at 11-Mar-14 18:44pm
   
Because it is integrated in to C# already, and if you're using SQL Server, your entire stack comes from the one vendor. If you're using another ORM, I'd not recommend changing, but EF is a good choice to start with on a new project.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 
//Set date out of query to compare
DateTime DateToCompare = DateTime.Now.AddHours(-12);
 
//Use FirstOrDefault(), it will return null value or only one record based on data
var customUser = (from u in context.Users
                join pr in context.PasswordRecoveries on u.Id equals pr.userId
                where pr.url == token && pr.requestDateTime < DateToCompare 
                select u).FirstOrDefault();
if(customUser != null)
{
//Success code
}
  Permalink  
v2
Comments
CHill60 at 12-Apr-14 7:19am
   
Your solution is incredibly similar to Solution 1 - if you're going to resurrect answered posts then it is a good idea to use some words to explain why rather than just essentially repeating someone else's answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I would suggest that use sql statements in the form of stored procedures. It will give you better performance. Another point is that its always easy to troubleshoot sql statements than linq statements.
 
For step by step instructions on how to use stored procedures in entity framework, check this article:
 
How to use Stored Procedures in Entity Framework[^]
  Permalink  

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

  Print Answers RSS
0 Marcin Kozub 265
1 Richard MacCutchan 239
2 OriginalGriff 208
3 Sergey Alexandrovich Kryukov 185
4 Praneet Nadkar 167
0 OriginalGriff 7,913
1 Sergey Alexandrovich Kryukov 7,232
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 27 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100