Solved

LINQ to SQL

Posted on 2013-06-06
7
720 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now