문제

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
도움이 되었습니까?

해결책 2

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))})

다른 팁

How's this?

from d in dps_admin_user
join c in dps_admin_role on d.user_id equals c.user_id into gcs
from c2 in gcs.DefaultIfEmpty()
group d.firstname by c2.parent_id into gfns
where gfns.Any()
orderby gfns.Count()
select gfns

I just threw it together in LINQPad. Not very meaningful I must say, but it contains your required operators.

Why do you need such an example??


This is roughly the query you want. I couldn't test it, but it should be fairly close.

var query =
    from n in nse_fo_transaction
    where n.client_id == 5
    where n.trade_date >= d0 && n.trade_date < d1
    join c0 in client_master on n.client_id equals c.client_id into cs
    from c in cs.DefaultIfEmpty()
    group new { n, c } by new
    {
        c.client_name,
        n.instrument_group_id,
        n.trade_date,
    } into gs
    orderby gs.Key.trade_date
    select new
    {
        gs.Key.client_name,
        gs.Key.instrument_group_id,
        gs.Key.trade_date,
        TotBuyQty = gs.Sum(x =>
            (float)x.n.buy_qty),
        TotBuyVal = gs.Sum(x => 
            (float)x.n.buy_value
            + (float)x.n.buy_brokerage),
        TotSellQty = gs.Sum(x => 
            (float)x.n.sell_qty),
        TotSellVal = gs.Sum(x =>
            (float)x.n.sell_value
            - (float)x.n.sell_brokerage),
        ProfitLoss = gs.Sum(x =>
            (float)x.n.sell_value
            - (float)x.n.sell_brokerage
            - (float)x.n.buy_value 
            + (float)x.n.buy_brokerage)
    };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top