Search code examples
linq-to-dataset

Linq to sql query fails


I am having problem in executing a linq to sql query. Please have a look.

Here is my code:

    SELECT c.client_name,
       n.instrument_group_id,
       n.trade_date,
       sum(n.buy_qty) AS TotBuyQty,
       sum(convert(float,n.buy_value) + convert(float,n.buy_brokerage)) AS TotBuyVal,
       sum(n.sell_qty) AS TotSellQty,
       sum(convert(float,n.sell_value) - convert(float,n.sell_brokerage)) AS TotSellVal,
       sum(convert(float,n.sell_value) - convert(float,n.sell_brokerage))- sum(convert(float,n.buy_value) + convert(float,n.buy_brokerage)) AS ProfitLoss
FROM nse_fo_transaction AS n
LEFT JOIN client_master AS c ON n.client_id = c.client_id
WHERE n.client_id = 5
  AND n.trade_date BETWEEN '09/01/2012' AND '09/19/2012'
GROUP BY c.client_name,
         n.instrument_group_id,
         n.trade_date
ORDER BY n.trade_date

Solution

  • Dim query = (From n In tblNseFo.AsEnumerable _
                                Where n!client_id = intClientID _
                                Group Join c In tblClient _
                                On n!client_id Equals c!client_id Into cs = Group _
                                From c In cs.DefaultIfEmpty _
                                Group n, c By _
                                c!client_name, n!instrument_group_id, n!trade_date Into gs = Group _
                                Order By trade_date _
                                Select New With { _
                                    .client_name = client_name, _
                                    .instrument_group_id = instrument_group_id, _
                                    .trade_date = trade_date, _
                                    .TotBuyQty = gs.Sum(Function(x) x.n!buy_qty), _
                                    .TotSellQty = gs.Sum(Function(x) x.n!sell_qty), _
                                    .TotBuyVal = gs.Sum(Function(x) x.n!buy_value) + gs.Sum(Function(x) x.n!buy_brokerage), _
                                    .TotSellVal = gs.Sum(Function(x) x.n!sell_value) - gs.Sum(Function(x) x.n!sell_brokerage), _
                                    .ProfitLoss = (gs.Sum(Function(x) x.n!sell_value) - gs.Sum(Function(x) x.n!sell_brokerage)) - _
                                                  (gs.Sum(Function(x) x.n!buy_value) + gs.Sum(Function(x) x.n!buy_brokerage))})