Need Help Converting Sql Query to EF

Nick R 66 Reputation points
2022-03-03T18:52:23.203+00:00

SELECT DISTINCT Item.ItemId
, Item.ITEMNAME
FROM DP_AX_PROD.dbo.PRODTABLE Prod
INNER JOIN DP_AX_PROD.dbo.INVENTTABLE Item
ON Prod.ItemId = Item.ItemId
AND Prod.DataAreaid = Item.DataAreaID
WHERE LEFT(Item.ITEMGROUPID, 7) = 'RMTHFLM'
AND Prod.DataAreaid = 'PROD'

So far I have:

var query = (from prod in db.ProdTable
join item in db.InventTable on prod.ItemId equals item.ItemID
select prod).Distinct();

            return query.ToList();

Any help would be greatly appreciated. Thank you!

Developer technologies | .NET | Other
{count} votes

Answer accepted by question author
  1. Jack J Jun 25,316 Reputation points
    2022-03-04T05:52:20.53+00:00

    @Nick R , based on my test, you could refer to the following code to convert Sql Query to EF .

    Code example:

     TestDBEntities db=new TestDBEntities();  
                var query = (from prod in db.ProdTables.ToList()  
                             join item in db.InventTables.ToList() on new { prod.ItemId, prod.DataAreaID } equals new { item.ItemId, item.DataAreaID }  
                             where item.ItemGroup.ToString().Substring(0, 7) == "RMTHFLM" && prod.DataAreaID == "Prod"  
                             select new  
                             {  
                                 item.ItemId,  
                                 item.ItemName  
                             }).Distinct().ToList() ;  
    

    Result in sql and in ef:

    179967-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.