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. linq equivalent of Sql stmt? [modified]

linq equivalent of Sql stmt? [modified]

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqhelpquestion
4 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.
  • M Offline
    M Offline
    mark_me
    wrote on last edited by
    #1

    Hi, I need help with linq for the follwoing statement. "Select productid,productname, sum(productqty) from product join orders on orders.productid=product.id group by productid,productname order by productname asc" Can i make this select from a listOrderProducts. if it is possible from listorderproducts then my statement should be "select(productid,productname,sum(productqty) from listorderproducts group by productid" I hve a class name product. and want to save the above in the list Thanx

    modified on Monday, June 22, 2009 12:59 PM

    E 1 Reply Last reply
    0
    • M mark_me

      Hi, I need help with linq for the follwoing statement. "Select productid,productname, sum(productqty) from product join orders on orders.productid=product.id group by productid,productname order by productname asc" Can i make this select from a listOrderProducts. if it is possible from listorderproducts then my statement should be "select(productid,productname,sum(productqty) from listorderproducts group by productid" I hve a class name product. and want to save the above in the list Thanx

      modified on Monday, June 22, 2009 12:59 PM

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      I guess that's what you want. Untested code alert :)

              var ctx = new Context();
              var res = ctx.Products.Select(product => new
              {
                  product.Id,
                  product.ProductName,
                  OrderedQty = ctx.Orders.Where(order => order.ProductId == product.Id)
                                         .Sum(order => order.ProductQty)
              });
      

      Assuming something like that.

      class Product
      {
      public int Id;
      public string ProductName;
      }

      class Order
      {
          public int ProductId;
          public int ProductQty;
      }
      
      class Context
      {
          public IEnumerable<Product> Products;
          public IEnumerable<Order> Orders;
      }
      

      Eslam Afifi

      modified on Tuesday, June 23, 2009 12:21 PM

      M 1 Reply Last reply
      0
      • E Eslam Afifi

        I guess that's what you want. Untested code alert :)

                var ctx = new Context();
                var res = ctx.Products.Select(product => new
                {
                    product.Id,
                    product.ProductName,
                    OrderedQty = ctx.Orders.Where(order => order.ProductId == product.Id)
                                           .Sum(order => order.ProductQty)
                });
        

        Assuming something like that.

        class Product
        {
        public int Id;
        public string ProductName;
        }

        class Order
        {
            public int ProductId;
            public int ProductQty;
        }
        
        class Context
        {
            public IEnumerable<Product> Products;
            public IEnumerable<Order> Orders;
        }
        

        Eslam Afifi

        modified on Tuesday, June 23, 2009 12:21 PM

        M Offline
        M Offline
        mark_me
        wrote on last edited by
        #3

        thanx

        E 1 Reply Last reply
        0
        • M mark_me

          thanx

          E Offline
          E Offline
          Eslam Afifi
          wrote on last edited by
          #4

          Glad to help.

          Eslam Afifi

          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