LINQ to SQL多表离开了连接

时间:2016-11-08 09:33:26

标签: c# sql-server linq

我有一个带有动态查询的存储过程,在我的情况下工作正常,但是当它没有使用edmx创建返回类型时。因为我正在尝试使用linq进行相同的存储过程,但由于我是新手而变得有点困难。任何人都可以帮助我。

这是我的代码

SQL:

Create PROCEDURE [dbo].[usp_GetProductList_SearchForManyToOne] 
@orgid bigint,   
 @ownerid bigint,
 @OrderList nvarchar(MAX)
AS

BEGIN    
-- -- SET NOCOUNT ON added to prevent extra result sets from  
-- -- interfering with SELECT statements.\  
SET NOCOUNT ON;

Declare @Sql nvarchar(max) = ''      
  SET @Sql = 'SELECT ProductID = ii.ProductId,
   InvItemId = convert(bigint,0),
   Name = p.Name,
   PrimaryImageID = p.PrimaryImageID,ProductNumberLabel = p.ProductNumberLabel,ProductNumber = p.ProductNumber,
   category = isnull(c.Name,''''),
   qty = ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00),
   SalePrice= ISNULL(p.SalePrice, 0.00),
   EnteredQuantity=(case when (ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00) > 1) then 1.00 else ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00) end)
   ,Discount=0,u.UnitName,
   u.UnitID     
   FROM dbo.[InventoryItems] ii  
   Left Join dbo.[Product] p on ii.ProductId = p.ProductId  and p.activestatus=1
   Left Join dbo.[category] c on p.DefaultCategoryId = c.CategoryId 
   Left Join dbo.[Unit] u on p.UnitId=u.UnitId and u.Activestatus=1 
   WHERE p.OrganizationID = '+ CAST(@orgid AS VARCHAR(15))+'
   AND ii.ActiveStatus = 1   
   AND p.ActiveStatus = 1  
   AND p.IsDisabled = 0  
   And p.CanSale = 1
   AND ii.InventoryID IN (' + @OrderList + ') 

   group by ii.ProductId, p.Name, p.PrimaryImageID, p.ProductNumberLabel, p.ProductNumber, c.Name,p.SalePrice,u.unitname,u.UnitID  
   having ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0) > 0  
   Order by p.ProductNumber, p.Name, c.Name '
--exec sp_executesql @SQL
EXEC(@SQL)
END 

在这里我正在尝试的是:

var qu= from ii in db.InventoryItems                            
                                join p in db.Products 
                                     on ii.ProductID equals p.ProductID into pGroup
                                from mr in pGroup.DefaultIfEmpty()

                                join u in db.Units
                                     on mr.UnitID equals u.UnitID into uGroup
                                from ur in uGroup.DefaultIfEmpty()

                                join c in db.Categories
                                     on mr.DefaultCategoryID equals c.CategoryID into cGroup
                                from cr in cGroup.DefaultIfEmpty()
                                where mr.ActiveStatus == 1 && mr.OrganizationID == orgID && ii.ActiveStatus == 1 

从现在开始,我无法理解如何做到这一点。

0 个答案:

没有答案