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. LINQ
  4. Count of Records

Count of Records

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorialquestion
3 Posts 3 Posters 14 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.
  • M Offline
    M Offline
    macca24
    wrote on last edited by
    #1

    I have a 20 records in a database table. Each record has and ID. I want to read a field in each record fieldA. I only want to read the records that have an ID '1'. For each record that has an ID '1' and fieldA Is Not Null I want to return a count of those records. So if 10 records have data in fieldA and have ID '1' then my count for fieldA will be 10. Anyone know how to do this in Linq?

    Richard DeemingR M 2 Replies Last reply
    0
    • M macca24

      I have a 20 records in a database table. Each record has and ID. I want to read a field in each record fieldA. I only want to read the records that have an ID '1'. For each record that has an ID '1' and fieldA Is Not Null I want to return a count of those records. So if 10 records have data in fieldA and have ID '1' then my count for fieldA will be 10. Anyone know how to do this in Linq?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You have already posted this in the C# forum[^], and received a perfectly decent answer.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • M macca24

        I have a 20 records in a database table. Each record has and ID. I want to read a field in each record fieldA. I only want to read the records that have an ID '1'. For each record that has an ID '1' and fieldA Is Not Null I want to return a count of those records. So if 10 records have data in fieldA and have ID '1' then my count for fieldA will be 10. Anyone know how to do this in Linq?

        M Offline
        M Offline
        Maciej Los
        wrote on last edited by
        #3

        It's possible to achieve that using Linq To Dataset[^]. Assuming that you did Load[^] data into DataTable object via using SqlReader[^]:

        var result = dt.AsEnumerable()
        .GroupBy(a=>a.Field("ID"))
        .Select(g=>new
        {
        Id = g.Key,
        Count = g.Count()
        })
        .ToList();

        Returns a list of Id's with the number of occurences. For further information, please see: Programming Guide (LINQ to DataSet)[^] Queries in LINQ to DataSet[^] Querying DataSets (LINQ to DataSet)[^] LINQ to DataSet Examples[^]

        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