Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

LINQ to SQL

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
mani_sai
Asked:
mani_sai
  • 5
  • 2
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
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
 
mani_saiAuthor Commented:
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
 
Meir RivkinFull stack Software EngineerCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mani_saiAuthor Commented:
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
 
mani_saiAuthor Commented:
Hello sedgwick,
Thanks

What namespace to use for EntityFunctions?

Thanks
0
 
mani_saiAuthor Commented:
got it.

System.Data.Objects.

Thanks
0
 
mani_saiAuthor Commented:
Awesome. It worked. Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now