0

i have a stored procedure with dynamic query which s working fine in my case but when it is not creating a return type with edmx. for that i am trying to use linq for the same stored procedure but getting a bit difficult as i am a newbie. can anyone help me on this.

here is my code

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 

and here what i am trying is:

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 

from now onwards i cant understand how to do that.

Miranda
  • 259
  • 1
  • 8
  • 19
  • Call ToString on qu and see what is the difference after that try to fix it. If you produce smaller problem than this it will be easier to help you. – mybirthname Nov 08 '16 at 09:36
  • Download [LINQPad](https://www.linqpad.net/) it shows you the SQL produced by your query so you can compare this to what you expect. (I have no affiliation to this, I just find it incredibly useful). – ChrisF Nov 08 '16 at 09:45
  • Below link can help you http://stackoverflow.com/questions/267488/linq-to-sql-multiple-left-outer-joins?rq=1 – User Nov 08 '16 at 10:04

0 Answers0