Solved

LINQ to SQL

Posted on 2013-06-06
7
750 Views
Last Modified: 2013-06-06
Hello Experts,
 i need to write LINQ to SQL:  (C#)

select MAX(CreateDate) from Ecommerce.dbo.EcomRegisterOpenCloseLookup where EcomTransactionTypeID = 9
and createdate < dateadd(HOUR,-1,@maxendDate)

Thanks
Mani
0
Comment
Question by:mani_sai
  • 5
  • 2
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39225832
i assume maxendDate is string so i used DateTime.Parse().
if it's DateTime then use lookup.createdate < maxendDate.AddHours(-1).
var maxCreatedDate = (from lookup in Ecommerce.dbo.EcomRegisterOpenCloseLookup 
where lookup.EcomTransactionTypeID = 9 and lookup.createdate < DateTime.Parse(maxendDate.ToString()).AddHours(-1)).Max();

Open in new window

0
 
LVL 8

Author Comment

by:mani_sai
ID: 39225855
Thanks.

I am getting error:

LINQ to Entities does not recognize the method 'System.DateTime AddHours(Double)' method, and this method cannot be translated into a store expression.

-------------------------------------

 private DateTime GetStartDate(DateTime endDate)
        {
           var getStartDate = (from ecom in _ecommerceDataConnection.EcomRegisterOpenCloseLookups
                                where ecom.EcomTransactionTypeID == 9 &&
                                ecom.CreateDate < DateTime.Parse(endDate.ToString()).AddHours(-1)
                                select (ecom.CreateDate)).Max();

           return getStartDate;

        }

--------------------------------



any idea?


Thanks
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39225885
change this to:
 private DateTime GetStartDate(DateTime endDate)
        {
           var getStartDate = (from ecom in _ecommerceDataConnection.EcomRegisterOpenCloseLookups
                                where ecom.EcomTransactionTypeID == 9 &&
                                ecom.CreateDate < EntityFunctions.AddHours(endDate, -1).Value
                                select (ecom.CreateDate)).Max(); 

           return getStartDate;

        }

Open in new window

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 8

Author Comment

by:mani_sai
ID: 39225889
I tried something like this:

var getStartDate = (from ecom in _ecommerceDataConnection.EcomRegisterOpenCloseLookups
                                where ecom.EcomTransactionTypeID == 9 &&
                                ecom.CreateDate < endDate.AddHours(Convert.ToDouble(-1))
                                select (ecom.CreateDate)).Max();

still the same error...

LINQ to Entities does not recognize the method 'System.DateTime AddHours(Double)' method, and this method cannot be translated into a store expression.

Thanks
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39225897
Hello sedgwick,
Thanks

What namespace to use for EntityFunctions?

Thanks
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39225902
got it.

System.Data.Objects.

Thanks
0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39225919
Awesome. It worked. Thanks.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question