Linq and the Let command

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ilyas

    #1

    Linq and the Let command

    Hi all

    In the northwind database, on the orders table, How would I get back
    the the number of items that exist with the following condition:

    ShipVia=1
    ShipVia=2
    ShipVia=3

    I want to do this in one database hit

    I have tried:

    Northwindcontex t c=new Northwindcontex t();
    var (from x in c.Orders
    let total1=c.Orders .Count(y=>y.Shi pVia=1)
    let total2=c.Orders .Count(y=>y.Shi pVia=2)
    let total3=c.Orders .Count(y=>y.Shi pVia=3)
    ).SingleOrDefau lt();

    but this doesnt work - I know why because I get an entry for every row
    in the orders table, but how do use the Let command so that I get just
    one row back?

    Many thanks

  • Frans Bouma [C# MVP]

    #2
    Re: Linq and the Let command

    Ilyas wrote:
    Hi all
    >
    In the northwind database, on the orders table, How would I get back
    the the number of items that exist with the following condition:
    >
    ShipVia=1
    ShipVia=2
    ShipVia=3
    >
    I want to do this in one database hit
    >
    I have tried:
    >
    Northwindcontex t c=new Northwindcontex t();
    var (from x in c.Orders
    let total1=c.Orders .Count(y=>y.Shi pVia=1)
    let total2=c.Orders .Count(y=>y.Shi pVia=2)
    let total3=c.Orders .Count(y=>y.Shi pVia=3)
    ).SingleOrDefau lt();
    >
    but this doesnt work - I know why because I get an entry for every row
    in the orders table, but how do use the Let command so that I get just
    one row back?
    What you seem to want is a query with 3 scalar queries in the
    projection (3 count queries on orders) and the FROM clause is really not
    important, as the data returned is retrieved in the scalars.

    This is inefficient.

    What you should use is something like:
    SELECT COUNT(*) as Amount, ShipVia
    FROM Orders
    WHERE ShipVia IN (1, 2, 3)
    GROUP BY ShipVia

    which gives you 3 rows, and the query is efficient.

    This is in linq something like:

    var q = from o in ctx.Orders
    where new int[] {1, 2, 3}.Contains(o.S hipVia)
    group o by o.ShipVia into g
    select new { Amount = g.Count(), ShipVia = g.Key};


    FB
    --
    ------------------------------------------------------------------------
    Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
    LLBLGen Pro website: http://www.llblgen.com
    My .NET blog: http://weblogs.asp.net/fbouma
    Microsoft MVP (C#)
    ------------------------------------------------------------------------

    Comment

    • Paul Musso

      #3
      Re: Linq and the Let command

      Ilyas a formulé ce mardi :
      Hi all
      >
      In the northwind database, on the orders table, How would I get back
      the the number of items that exist with the following condition:
      >
      ShipVia=1
      ShipVia=2
      ShipVia=3
      >
      I want to do this in one database hit
      >
      I have tried:
      >
      Northwindcontex t c=new Northwindcontex t();
      var (from x in c.Orders
      let total1=c.Orders .Count(y=>y.Shi pVia=1)
      let total2=c.Orders .Count(y=>y.Shi pVia=2)
      let total3=c.Orders .Count(y=>y.Shi pVia=3)
      ).SingleOrDefau lt();
      >
      but this doesnt work - I know why because I get an entry for every row
      in the orders table, but how do use the Let command so that I get just
      one row back?
      >
      Many thanks
      Hi Ilyas,

      Tryed to do something like that :

      (from x in c.Orders
      select new {
      Total1=c.Orders .Count(y=>yShip Via==1)
      Total2=c.Orders .Count(y=>yShip Via==2)
      Total3=c.Orders .Count(y=>yShip Via==3)
      }).FirstOrDefau lt();

      ?

      --
      Paul Musso


      Comment

      • Peter Morris

        #4
        Re: Linq and the Let command

        I don't know the syntax, but I think a better approach would be to group

        (in sql)
        select count(*), ShipVia from orders where ShipVia in (1,2,3) group by
        ShipVia;



        Comment

        Working...