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. Need help with code

Need help with code

Scheduled Pinned Locked Moved LINQ
databasequestionhelp
3 Posts 2 Posters 4 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
    Nigel Mackay
    wrote on last edited by
    #1

    Table Parts has PartID as identity Table Orders has a field PartID as a foreign key As the stock of a part drops below OrderLevel it gets ordered and delivered. So many orders for this part. Come end of the month I need a list of Parts where Stock < OrderLevel, but no outstanding Order exists. This was easy using SQL and Datasets - just delete the row in the dataset if there is an Order for that PartID where Received < Ordered. But if I use

    neededParts = from np in MainMenu.db.Parts
    where np.Stock < np.OrderLevel
    orderby np.StoreNumber
    select np;

    and then identify PartID's that need to be removed, how do I do the removal? I could add a field to Parts, AreNeeded, and set that flag and then put that in the where statement. Doesn't seem very elegant.

    S 1 Reply Last reply
    0
    • N Nigel Mackay

      Table Parts has PartID as identity Table Orders has a field PartID as a foreign key As the stock of a part drops below OrderLevel it gets ordered and delivered. So many orders for this part. Come end of the month I need a list of Parts where Stock < OrderLevel, but no outstanding Order exists. This was easy using SQL and Datasets - just delete the row in the dataset if there is an Order for that PartID where Received < Ordered. But if I use

      neededParts = from np in MainMenu.db.Parts
      where np.Stock < np.OrderLevel
      orderby np.StoreNumber
      select np;

      and then identify PartID's that need to be removed, how do I do the removal? I could add a field to Parts, AreNeeded, and set that flag and then put that in the where statement. Doesn't seem very elegant.

      S Offline
      S Offline
      Syed Mehroz Alam
      wrote on last edited by
      #2

      So you want to get parts that have no matching row in the Orders table. Here's a try:

      from np in MainMenu.db.Parts
      where
      np.Stock < np.OrderLevel
      &&
      (
      from o in MainMenu.db.Orders where o.PartID == np.PartID select o
      ).Count() == 0
      orderby np.StoreNumber
      select np;

      Hope that helps. Regards,

      Syed Mehroz Alam My Blog | My Articles
      Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

      N 1 Reply Last reply
      0
      • S Syed Mehroz Alam

        So you want to get parts that have no matching row in the Orders table. Here's a try:

        from np in MainMenu.db.Parts
        where
        np.Stock < np.OrderLevel
        &&
        (
        from o in MainMenu.db.Orders where o.PartID == np.PartID select o
        ).Count() == 0
        orderby np.StoreNumber
        select np;

        Hope that helps. Regards,

        Syed Mehroz Alam My Blog | My Articles
        Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

        N Offline
        N Offline
        Nigel Mackay
        wrote on last edited by
        #3

        Partial solution! There will be lots of entries where a specific PartID was ordered and delivered. But now they need ordering again. As written, only allows parts that have never been ordered. So needs one more clause only zapping PartID's that have oo.Ordered == oo.Received So I have changed it to

        neededParts = from np in MainMenu.db.Parts
        where np.Stock < np.OrderLevel
        &&
        (
        from oo in MainMenu.db.Orders
        where (oo.PartID == np.PartID) && (oo.Ordered==oo.Received)
        select oo
        ).Count() == 0
        orderby np.StoreNumber
        select np;

        and it works just the way I want. Thanks for the help. It gives the solution to other posiible scenarios as well.

        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