如何将这个SQL查询转换为LINQ?

时间:2018-07-22 06:51:11

标签: sql entity-framework linq

考虑以下查询:

 SELECT IT.TotalPrice,IT.SellerBrokerId,IT.BuyerBrokerId,C.Broker,C.Seo,C.Ime,C.OrderSideId
                       FROM Brs.ImeTrade AS IT 
                       INNER JOIN Brs.ImeCommodity AS IC  ON IC.Id = IT.ImeCommodityId
                       INNER JOIN Brs.ImeCommoditySubGroup AS ICSG ON ICSG.Id = IC.ImeCommoditySubGroupId
                       INNER JOIN Brs.ImeCommodityGroup AS ICG ON ICG.Id = ICSG.ImeCommodityGroupId
                       INNER JOIN Brs.StockTypeMarketType AS M ON M.StockTypeMarketId = ICG.StockTypeMarketId
                       INNER JOIN Brs.StockTypeMarketTypeCommission AS C 
                            ON C.IsMarketMaker = 0
                            AND ( C.OrderSideId = CASE SellerBrokerId WHEN 44  THEN 2 END  OR C.OrderSideId = CASE BuyerBrokerId WHEN 44  THEN 1 END ) 
                            AND C.StockTypeMarketTypeId = M.Id
    WHERE it.SettlementDate IS NOT NULL

我想为实体框架创建LINQ查询

3 个答案:

答案 0 :(得分:2)

您可以尝试此代码。

   var results = from IT in _context.ImeTrades
                join IC in _context.ImeCommodities on new { Id = IT.ImeCommodityId } equals new { Id = IC.Id }
                join ICSG in _context.ImeCommoditySubGroups on new { Id = IC.ImeCommoditySubGroupId } equals new { Id = ICSG.Id }
                join ICG in _context.ImeCommodityGroups on new { Id = ICSG.ImeCommodityGroupId } equals new { Id = ICG.Id }
                join M in _context.StockTypeMarketTypes on new { StockTypeMarketId = ICG.StockTypeMarketId } equals new { StockTypeMarketId = (int?)M.StockTypeMarketId }
                join C in _context.StockTypeMarketTypeCommissions on new { StockTypesID = M.Id } equals new { StockTypesID = (byte)C.StockTypeMarketTypeId }
                where
                    IT.SettlementDate != null && C.IsMarketMaker == false && (C.OrderSideId == (IT.SellerBrokerId == 44 ? 2 : 1) || (C.OrderSideId == (IT.BuyerBrokerId == 44 ? 1 : 2)))
                select new
                {
                    IT.TotalPrice,
                    IT.SellerBrokerId,
                    IT.BuyerBrokerId,
                    C.Broker,
                    C.Seo,
                    C.Ime,
                    C.OrderSideId
                };

答案 1 :(得分:1)

这是代码段:

var results = context.tb1.Join(
                  context.tb2,
                  t1 => new { t1.Col1, t1.Col2, t1.Col3 },
                  t2 => new { t2.Col1, t2.Col2, t2.Col3 },
                  (t1, t2) => new { t1, t2 })
              .Where(o => o.t2.Col1 == col1 
                  && o.t2.Col2 == col2
                  && o.t2.Col4 == someString)
              .Select(o => o.t1);

或:

var result = from a in Context.tb1
             join h in Context.tb2 on a.id equals h.id
             join c in Context.tb3 on a.id equals c.id2
             where c.Type == "Type"
             select new {
                 a.ID,
                 a.id2,
                 h.id3,
                 a.id4,
                 a.id5,
                 c.id6,
                 a.id7 };

答案 2 :(得分:1)

尝试此代码:

var results = from IT in _context.ImeTrades
        join IC in _context.ImeCommodities on new { Id = IT.ImeCommodityId } equals new { Id = IC.Id }
        join ICSG in _context.ImeCommoditySubGroups on new { Id = IC.ImeCommoditySubGroupId } equals new { Id = ICSG.Id }
        join ICG in _context.ImeCommodityGroups on new { Id = ICSG.ImeCommodityGroupId } equals new { Id = ICG.Id }
        join M in _context.StockTypeMarketTypes on new { StockTypeMarketId = (int)ICG.StockTypeMarketId } equals new { StockTypeMarketId = M.StockTypeMarketId }
        join C in _context.StockTypeMarketTypeCommissions
                on new { IsMarketMaker = (bool)false, OrderSideId = IT.SellerBrokerId == 44 ? (long?)2 : null, Column1 = IT.BuyerBrokerId == 44 ? (long?)1 : null, StockTypeMarketTypeId = M.Id }
            equals new { C.IsMarketMaker, OrderSideId = (long?)C.OrderSideId, Column1 = (long?)C.OrderSideId, StockTypeMarketTypeId = (byte)C.StockTypeMarketTypeId }
        where
        IT.SettlementDate != null
        select new
        {
            IT.TotalPrice,
            IT.SellerBrokerId,
            IT.BuyerBrokerId,
            C.Broker,
            C.Seo,
            C.Ime,
            OrderSideId = (int?)C.OrderSideId
        };