使用DTO和linq-使用IF / THEN来展平webapi EF

时间:2016-11-30 17:26:43

标签: c# entity-framework linq asp.net-web-api

我试图使用DTO来平整我的webapi EF。到目前为止,我有以下声明正常工作。现在我想添加一层复杂性来说明IF / THEN。我在SELECT NEW SECTION的第一行写了一个假代码。 有人可以帮忙吗?

            var query = (
                        from acctTbl in db.Accounts
                        join tradeTbl in db.Trades on acctTbl.AccountID equals tradeTbl.AccountID into ts
                        from tradeTbl in ts.DefaultIfEmpty()
                        join mapClientAcct in db.Mapping_ClientAccounts on acctTbl.AccountID equals mapClientAcct.AccountID
                        join clientTbl in db.Clients on mapClientAcct.ClientID equals clientTbl.ClientID
                        join mapUserClient in db.Mapping_UserClients on clientTbl.ClientID equals mapUserClient.ClientID
                        join aspNetUser in db.AspNetUsers on mapUserClient.AspNetUsersID equals aspNetUser.Id
                        join mktData in db.MarketDatas on tradeTbl.MarketDataID equals mktData.MarketDataID into ms
                        from mktData in ms.DefaultIfEmpty()
                        join mktCode in db.GMI_MarketCodes on tradeTbl.GMI_MarketCodesID equals mktCode.GMI_MarketCodesID into mc
                        from mktCode in mc.DefaultIfEmpty()
                        join Mgrs in db.Managers on acctTbl.ManagerID equals Mgrs.ManagerID
                        join FxMkts in db.ForexMarkets on mktData.crncy equals FxMkts.CurrencySymbol into fm
                        from FxMkts in fm.DefaultIfEmpty()
                            where acctTbl.AccountActive == true
                                && clientTbl.ClientID == clientID
                                && aspNetUser.UserName == username
                            select new TradeDetailDTO()
                            {

                                --THIS IS WHAT I WANT TO DO!!!
                                IF tradeTblIdentifier == "F" THEN 'yes'
                                ELSE 'no'
                                -------------------------

                                Filedate = tradeTbl.Filedate,
                                Quantity = tradeTbl.Quantity,
                                Month = tradeTbl.Month,
                                Strike = tradeTbl.Strike,
                                PutCall = tradeTbl.PutCall,
                                Prompt = tradeTbl.Prompt,
                                StmtPrice = tradeTbl.Price,                                    
                                ShortDesc = mktCode.ShortDesc,
                                Sector = mktCode.Sector,
                                ExchName = mktCode.ExchName,
                                BBSymbol = mktData.BBSymbol,
                                BBName = mktData.Name,
                                fut_Val_Pt = mktData.fut_Val_Pt,
                                crncy = mktData.crncy,
                                fut_tick_size = mktData.fut_tick_size,
                                fut_tick_val = mktData.fut_tick_val,
                                fut_init_spec_ml = mktData.fut_init_spec_ml,
                                last_price = mktData.last_price,
                                bid = mktData.bid,
                                ask = mktData.ask,
                                px_settle_last_dt_rt = mktData.px_settle_last_dt_rt,
                                px_settle_actual_rt = mktData.px_settle_actual_rt,
                                chg_on_day = mktData.chg_on_day,
                                prev_close_value_realtime = mktData.prev_close_value_realtime,
                                AccountNumber = acctTbl.AccountNumber,
                                TradeLevel = acctTbl.TradeLevel,
                                ManagerName = Mgrs.ManagerName,
                                ManagerShortCode = Mgrs.ManagerShortCode,
                                ForexLastPrice = db.MarketDatas.FirstOrDefault(x => x.BBSymbol == mktData.crncy + " BGN CURNCY") == null ? 1: db.MarketDatas.FirstOrDefault(x => x.BBSymbol == mktData.crncy + " BGN CURNCY").last_price,
                                //ForexLastPrice = FxMkts.LastPrice,  ORIGINAL
                                TopdayIdentifier = "P",
                                DailyPercentage = acctTbl.DailyPercentage,
                                AccountType = acctTbl.AccountType
                            }
                    );

1 个答案:

答案 0 :(得分:1)

如果我理解正确你想要这样的事情:

var query = (
                        from acctTbl in db.Accounts
                        join tradeTbl in db.Trades on acctTbl.AccountID equals tradeTbl.AccountID into ts
                        from tradeTbl in ts.DefaultIfEmpty()
                        join mapClientAcct in db.Mapping_ClientAccounts on acctTbl.AccountID equals mapClientAcct.AccountID
                        join clientTbl in db.Clients on mapClientAcct.ClientID equals clientTbl.ClientID
                        join mapUserClient in db.Mapping_UserClients on clientTbl.ClientID equals mapUserClient.ClientID
                        join aspNetUser in db.AspNetUsers on mapUserClient.AspNetUsersID equals aspNetUser.Id
                        join mktData in db.MarketDatas on tradeTbl.MarketDataID equals mktData.MarketDataID into ms
                        from mktData in ms.DefaultIfEmpty()
                        join mktCode in db.GMI_MarketCodes on tradeTbl.GMI_MarketCodesID equals mktCode.GMI_MarketCodesID into mc
                        from mktCode in mc.DefaultIfEmpty()
                        join Mgrs in db.Managers on acctTbl.ManagerID equals Mgrs.ManagerID
                        join FxMkts in db.ForexMarkets on mktData.crncy equals FxMkts.CurrencySymbol into fm
                        from FxMkts in fm.DefaultIfEmpty()
                            where acctTbl.AccountActive == true
                                && clientTbl.ClientID == clientID
                                && aspNetUser.UserName == username
                            select new TradeDetailDTO()
                            {

                                YesNo = tradeTbl.Identifier == "F"?"yes":"no",
                                Filedate = tradeTbl.Filedate,
                                Quantity = tradeTbl.Quantity,
                                Month = tradeTbl.Month,
                                Strike = tradeTbl.Strike,
                                PutCall = tradeTbl.PutCall,
                                Prompt = tradeTbl.Prompt,
                                StmtPrice = tradeTbl.Price,                                    
                                ShortDesc = mktCode.ShortDesc,
                                Sector = mktCode.Sector,
                                ExchName = mktCode.ExchName,
                                BBSymbol = mktData.BBSymbol,
                                BBName = mktData.Name,
                                fut_Val_Pt = mktData.fut_Val_Pt,
                                crncy = mktData.crncy,
                                fut_tick_size = mktData.fut_tick_size,
                                fut_tick_val = mktData.fut_tick_val,
                                fut_init_spec_ml = mktData.fut_init_spec_ml,
                                last_price = mktData.last_price,
                                bid = mktData.bid,
                                ask = mktData.ask,
                                px_settle_last_dt_rt = mktData.px_settle_last_dt_rt,
                                px_settle_actual_rt = mktData.px_settle_actual_rt,
                                chg_on_day = mktData.chg_on_day,
                                prev_close_value_realtime = mktData.prev_close_value_realtime,
                                AccountNumber = acctTbl.AccountNumber,
                                TradeLevel = acctTbl.TradeLevel,
                                ManagerName = Mgrs.ManagerName,
                                ManagerShortCode = Mgrs.ManagerShortCode,
                                ForexLastPrice = db.MarketDatas.FirstOrDefault(x => x.BBSymbol == mktData.crncy + " BGN CURNCY") == null ? 1: db.MarketDatas.FirstOrDefault(x => x.BBSymbol == mktData.crncy + " BGN CURNCY").last_price,
                                //ForexLastPrice = FxMkts.LastPrice,  ORIGINAL
                                TopdayIdentifier = "P",
                                DailyPercentage = acctTbl.DailyPercentage,
                                AccountType = acctTbl.AccountType
                            }
                    );