Dmitry Guly Dmitry Guly - 1 year ago 58
SQL Question

Converting T-sql query into linq

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

SELECT, [Products].Name
[AvailableProducts] ON [AvailableProducts].IdProduct = [Products].Id
AND [AvailableProducts].IdUser = 'daa8ee71-f1d7-4b41-aa1c-464ae730dd63'
([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;

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

Answer Source

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