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. How to sum field in already exist Linq result [modified]

How to sum field in already exist Linq result [modified]

Scheduled Pinned Locked Moved LINQ
tutorialcsharpdatabasesql-serverlinq
2 Posts 1 Posters 3 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.
  • S Offline
    S Offline
    Sam Martini
    wrote on last edited by
    #1

    I need to sum field depending on 2 tables with 1 to many relations

    customer
    Id name
    1 a
    2 b
    3 c

    Stock
    CustID qty
    1 10
    1 15
    2 20

    I have somewhere in my project the following

    Dim lqcus = From c In db0.customers Where c.id = txt_idcust.Text Select c

    I managed to sum by 2 ways

    Dim sum1 = (From s In db0.stocks Where s.CustID = txt_idcust.Text Select c.qty).Sum
    Dim sum2 = Aggregate s In db0.stocks Where s.CustID = txt_idcust.Text Into sum(s.qty)

    But the above 2 will be executed again in the sql server to get the result, but I already have the lqcus which is linq result for the customer and inside the lqcus there is the stock already filtered by the 1 to many relation For example, I can write the following

    Dim sum3 = From s In lqcus.stocks Where s.CustID = txt_idcust.Text

    But I could not find the syntax to sum the qty Can someone help?

    modified on Tuesday, July 19, 2011 6:36 AM

    S 1 Reply Last reply
    0
    • S Sam Martini

      I need to sum field depending on 2 tables with 1 to many relations

      customer
      Id name
      1 a
      2 b
      3 c

      Stock
      CustID qty
      1 10
      1 15
      2 20

      I have somewhere in my project the following

      Dim lqcus = From c In db0.customers Where c.id = txt_idcust.Text Select c

      I managed to sum by 2 ways

      Dim sum1 = (From s In db0.stocks Where s.CustID = txt_idcust.Text Select c.qty).Sum
      Dim sum2 = Aggregate s In db0.stocks Where s.CustID = txt_idcust.Text Into sum(s.qty)

      But the above 2 will be executed again in the sql server to get the result, but I already have the lqcus which is linq result for the customer and inside the lqcus there is the stock already filtered by the 1 to many relation For example, I can write the following

      Dim sum3 = From s In lqcus.stocks Where s.CustID = txt_idcust.Text

      But I could not find the syntax to sum the qty Can someone help?

      modified on Tuesday, July 19, 2011 6:36 AM

      S Offline
      S Offline
      Sam Martini
      wrote on last edited by
      #2

      I manage to solve it.

      Dim sum3 = Aggregate s In lqcus.stocks Into Sum(s.qty)

      Thanks to every one who look at it.

      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