Click here to Skip to main content
15,178,226 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I trying to select user record using

SQL
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
Comments
[no name] 11-Oct-13 8:30am
   
What is the problem u are facing...??
Simon_Whale 11-Oct-13 10:10am
   
You need to explain this more as technically there is nothing wrong with the query on its own.
Member 12119093 18-Jan-19 12:25pm
   
Here is https://www.tektutorialshub.com/linq-to-entities/join-query-entity-framework/ you can refer to

You need to tell us what errors you get, but EF does not like functions inside it. Do this instead:

XML
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.
   
Comments
gaswent 10-Mar-14 3:18am
   
why to choose entity framework when other orms in .net
Christian Graus 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.
C#
//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
}
   
v2
Comments
CHill60 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
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[^]
   

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