LINQ - 如何在单个查询中使用left join,group by,where子句和order by进行linq查询

时间:2012-09-19 03:53:28

标签: linq-to-dataset

我用Google搜索了四天,但没有找到一个包含单个linq查询中所有三个关键字的示例。

这是我的代码:

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 个答案:

答案 0 :(得分:0)

这是怎么回事?

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

我只是把它放在LINQPad中。我必须说,这不是很有意义,但它包含您所需的操作符。

为什么你需要这样的例子?


这大致是您想要的查询。我无法测试它,但它应该相当接近。

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

答案 1 :(得分:0)

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