Convert Sql to LINQ complex C# function to readable sql server string -
i read lot getting readable sql server query string c# linq sql function, not figure out how so, maybe because project working on complicated.
i'm pasting pice of code want convert sql (select * bonds join etc.)
public list<entbondportfolio> getbondsforportfolio(list<int> groups, list<int> ratings, list<int> collaterals, list<int> companies, list<int> countries, list<int> fields, list<int> currencies, datetime? fromexpirationdate, datetime? untilexpirationdate, bool? perpetual, int? sortorder = 1) { // bonds return (from bond in dallinqclasses.instance.bonds join company in dallinqclasses.instance.companies on bond.company_id equals company.companyid companyjoin companyj in companyjoin.defaultifempty() join sellbuy in dallinqclasses.instance.sell_buy_operations on bond.bondid equals sellbuy.investment_id join account in dallinqclasses.instance.bank_accounts on sellbuy.bankaccount_id equals account.bankaccountid join groupstable in dallinqclasses.instance.groups on account.groupaccount_id equals groupstable.groupid join customer in dallinqclasses.instance.customers on groupstable.customer_id equals customer.customerid join rating in dallinqclasses.instance.ratings on bond.rating_id equals rating.ratingid ratingjoin ratingj in ratingjoin.defaultifempty() join currency in dallinqclasses.instance.currencies on bond.currency_id equals currency.currencyid join country in dallinqclasses.instance.countries on bond.country_id equals country.countryid countryjoin countryj in countryjoin.defaultifempty() join field in dallinqclasses.instance.fields on bond.field_id equals field.fieldid fieldjoin fieldj in fieldjoin.defaultifempty() join collateral in dallinqclasses.instance.risc_levels on bond.risclevel_id equals collateral.risclevelid collateraljoin collateralj in collateraljoin.defaultifempty() join orderi in // order dallinqclasses.instance.portfolio_orders.where(x => x.investmenttype_id == (int)investment_types.bonds) on new enttwoints { bankaccountid = bond.bondid, groupid = groupstable.groupid } equals new enttwoints { bankaccountid = orderi.investment_id, groupid = orderi.group_id } orderjoin orderj in orderjoin.defaultifempty() sellbuy.investmenttype_id == (int)investment_types.bonds && groups.contains(groupstable.groupid) && (collaterals.contains(bond.risclevel_id ?? -1) || collaterals.contains(-1)) && (ratings.contains(bond.rating_id ?? -1) || ratings.contains(-1)) && (companies.contains(bond.company_id ?? -1) || companies.contains(-1)) && (countries.contains(bond.country_id ?? -1) || countries.contains(-1)) && (fields.contains(bond.field_id ?? -1) || fields.contains(-1)) && (currencies.contains(bond.currency_id) || currencies.contains(-1)) && (bond.expirationdate >= fromexpirationdate || fromexpirationdate == null) && (bond.expirationdate <= untilexpirationdate || untilexpirationdate == null) && (perpetual == bond.perpetual || perpetual == null) group sellbuy new { bond, companyj, groupstable, account, orderj, customer, ratingj, collateralj, countryj, fieldj } g orderby g.key.orderj != null ? false : true, g.key.orderj.sortorder, g.key.groupstable.groupname, g.key.companyj.companyname select new entbondportfolio { rating = g.key.ratingj.ratingname, risclevel = g.key.collateralj.risclevelname, group = g.key.groupstable.groupname, client = g.key.customer.firstname + ' ' + g.key.customer.lastname, groupid = g.key.groupstable.groupid, company = g.key.companyj.companyname, currencyid = g.key.bond.currency_id, bondid = g.key.bond.bondid, calltext = g.key.bond.calltext, field = g.key.fieldj.fieldname, country = g.key.countryj.countryname, couponpercent = g.key.bond.couponpercent, // total sum of coupons in checking account of current bond couponspaid = dallinqclasses.instance.checking_accounts. where(x => x.actiontype_id == (int)action_types.coupon && g.select(y => y.sellbuyoperationid). contains(x.sellbuyoperation_id.getvalueordefault())) .sum(s => s.actualsum), expirationdate = g.key.bond.expirationdate, facevalue = (g.where(x => x.issell == false).sum(x => x.facevalue) - (g.where(x => x.issell == true).sum(x => x.facevalue) ?? 0)), isin = g.key.bond.isin, lastbuydate = g.where(x => x.issell == false).max(x => x.sellbuyoperationdate), currentprice = dalinvestmenttypes.instance.getcurrentpriceofbond(g.key.bond.bondid, bonds_history_types.marketvalue), currentyield = dalinvestmenttypes.instance.getcurrentpriceofbond(g.key.bond.bondid, bonds_history_types.yield), currentacrinterest = dalinvestmenttypes.instance.getcurrentpriceofbond(g.key.bond.bondid, bonds_history_types.acrinterest), purchaseprice = g.where(x => x.issell == false).sum(x => x.facevalue) == 0 ? 0 : g.where(x => x.issell == false).sum(x => x.facevalue * x.price) / g.where(x => x.issell == false).sum(x => x.facevalue), instruction = dalinstructions.instance.isinvestmenthaveinstruction(investment_types.bonds, g.key.bond.bondid, g.key.account.bankaccountid), }).tolist(); }
Comments
Post a Comment