Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Requesting for a snippet that can be used in LINQ to SQL query

Requesting for a snippet that can be used in LINQ to SQL query

Scheduled Pinned Locked Moved C#
databaselinqcsharpperformance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nadia Monalisa
    wrote on last edited by
    #1

    Hi, In my code, I am counting the number of records (sent emails) that are older than one month. In order to do that, I am using following snippet.

    int totalSentEmailsOlderThanMonth = emailSendingService.ListSentEmails().ToArray().Count(x => (DateTime.Now - x.SentDateTime).TotalDays > 30);

    The reason I am using .ToArray() in my code as (DateTime.Now - x.SentDateTime).TotalDays cannot be translated in LINQ to SQL query so, by calling ToArray(), I am loading all records into the memory and then counting. But there are more than hundreds of records in my database and loading all the records into the memory just for counting is not efficient at all. So, would anyone please give me an alternative more efficient snippet that can be used in this scenario. [[ By the way, emailSendingService.ListSentEmails() returns IQueryable<MyRecordType> ]] Regards.

    K 1 Reply Last reply
    0
    • N Nadia Monalisa

      Hi, In my code, I am counting the number of records (sent emails) that are older than one month. In order to do that, I am using following snippet.

      int totalSentEmailsOlderThanMonth = emailSendingService.ListSentEmails().ToArray().Count(x => (DateTime.Now - x.SentDateTime).TotalDays > 30);

      The reason I am using .ToArray() in my code as (DateTime.Now - x.SentDateTime).TotalDays cannot be translated in LINQ to SQL query so, by calling ToArray(), I am loading all records into the memory and then counting. But there are more than hundreds of records in my database and loading all the records into the memory just for counting is not efficient at all. So, would anyone please give me an alternative more efficient snippet that can be used in this scenario. [[ By the way, emailSendingService.ListSentEmails() returns IQueryable<MyRecordType> ]] Regards.

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      You need to re-write the predicate a little bit (I don't have time to check this, so consider it not-very psuedo psuedo code) and it might error:

      DateTime latestSendDate = DateTime.Now.AddDays(-30);
      int totalSentEmailsOlderThanMonth = emailSendingService.ListSentEmails().Count(x => x.SentDateTime <= latestSendDate);

      This removes the "calculation" from the LINQ query count predicate and puts it into a format it should be able to generate SQL from. There may be an out by one error (you get 29/31 days). I think this will work, as I say, I don't have time to check it. Let me know if there are any problems.

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-
      A Dead ringer for Kate Winslett[^]

      N 1 Reply Last reply
      0
      • K Keith Barrow

        You need to re-write the predicate a little bit (I don't have time to check this, so consider it not-very psuedo psuedo code) and it might error:

        DateTime latestSendDate = DateTime.Now.AddDays(-30);
        int totalSentEmailsOlderThanMonth = emailSendingService.ListSentEmails().Count(x => x.SentDateTime <= latestSendDate);

        This removes the "calculation" from the LINQ query count predicate and puts it into a format it should be able to generate SQL from. There may be an out by one error (you get 29/31 days). I think this will work, as I say, I don't have time to check it. Let me know if there are any problems.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-
        A Dead ringer for Kate Winslett[^]

        N Offline
        N Offline
        Nadia Monalisa
        wrote on last edited by
        #3

        Owe, thats the cute idea. Thanks a loooot. Sure, it is a working code and it is working perfectly. Thank you very much for your help.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups