Dmitry Guly Dmitry Guly - 1 month ago 6
SQL Question

Converting T-sql query into linq

enter image description hereThe query I'm trying to translate is:

SELECT
Products.id, [Products].Name
FROM
[Products]
LEFT JOIN
[AvailableProducts] ON [AvailableProducts].IdProduct = [Products].Id
AND [AvailableProducts].IdUser = 'daa8ee71-f1d7-4b41-aa1c-464ae730dd63'
WHERE
([AvailableProducts].IdProduct IS NULL)


I'm trying to solve this problem this Linq code:

public void DataBindGriedProducts(string idUser)
{
List<Product> data = new List<Product>();

var con = from test in db.AvailableProducts
join product in db.Products on new { test.IdProduct, test.IdUser} equals new { product.Id, idUser}
select new
{
surname = product.Name,
};

grdConnectedProduct.DataSource = data;
grdConnectedProduct.DataBind();
}


But it's not correct. Help me, please!

Answer

I fixed it:

var o = (from p in db.Products
                join p2 in db.AvailableProducts on new { f1 = (int)p.Id, f2 = idUser } equals new { f1 = (int)p2.IdProduct, f2 = p2.IdUser }

                into temp
                from x in temp.DefaultIfEmpty()
                 where x.IdProduct == null
                 select new { f1 = p.Name});
Comments