【问题标题】:How can i convert this sql query to LINQ?如何将此 sql 查询转换为 LINQ?
【发布时间】:2018-07-22 06:51:10
【问题描述】:

考虑以下查询:

 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 查询

【问题讨论】:

  • 你能展示你的 EF 上下文吗?你试过什么?
  • 如果SellerBrokerId !=44 我的意思是ELSE 的情况会怎样
  • 我使用EF Code First,DbSet名称是tables的复数名称。D-Shih
  • 当 SellerBrokerId !=44 什么都不做时,Sumit raj

标签: sql entity-framework linq


【解决方案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 = 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
                };

【讨论】:

    【解决方案2】:

    这是sn-p:

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

    【讨论】:

    • sn-p,这些是你的表名
    • 我的问题是 INNER JOIN Brs.StockTypeMarketTypeCommission AS C
    【解决方案3】:

    试试这个代码:

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

    【讨论】:

    • 在 _context.StockTypeMarketTypeCommissions 中加入 C 无效
    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多