views:

86

answers:

4

Hello everybody,

I have a beginners LINQ2SQL question. I have this huge (but not complex) SQL statement:

SELECT Artikel.ArtikelID, 
       Artikel.CategorieID, 
       Artikel.ImageFile, 
       Artikel.RetailPrijs, 
       ISNULL(ShopArtikel.VerkoopsPrijs, Artikel.VerkoopsPrijs) AS VerkoopsPrijs, 
       Artikel.ArtikelCode, 
       Artikel.InAssortimentSinds, 
       ArtikelTaal.ArtikelNaam, 
       ArtikelTaal.ArtikelOmschrijving
FROM   Artikel 
INNER JOIN ArtikelTaal ON Artikel.ArtikelID = ArtikelTaal.ArtikelID 
INNER JOIN ShopArtikel ON Artikel.ArtikelID = ShopArtikel.ArtikelID 
INNER JOIN Categorie ON Artikel.CategorieID = Categorie.CategorieID 
INNER JOIN CategorieTaal ON Categorie.CategorieID = CategorieTaal.CategorieID 
INNER JOIN Shop ON ShopArtikel.ShopId = Shop.ShopID 
INNER JOIN CategorieGroepShop ON Shop.ShopID = CategorieGroepShop.ShopId 
INNER JOIN Taal ON ArtikelTaal.TaalCode = Taal.TaalCode AND CategorieTaal.TaalCode = Taal.TaalCode 
INNER JOIN CategorieGroepTaal ON Taal.TaalCode = CategorieGroepTaal.TaalCode AND CategorieGroepShop.CategorieGroepId = CategorieGroepTaal.CategorieGroepID 
INNER JOIN CategorieGroep ON Categorie.CategorieGroepID = CategorieGroep.CategorieGroepID AND CategorieGroepTaal.CategorieGroepID = CategorieGroep.CategorieGroepID AND CategorieGroepShop.CategorieGroepId = CategorieGroep.CategorieGroepID
WHERE (Shop.ShopID = 23) AND 
      (Taal.TaalCode = 'dut') AND 
      (Artikel.Onzichtbaar = 0) AND 
      (Artikel.NietBestelbaar = 0) AND 
      (Categorie.Onzichtbaar = 0) AND 
      (Artikel.Voorraad >= Artikel.LevertijdDrempel)

and I am converting this to LINQ and have this:

var allProducts = from artikelen in dc.Artikels
join sa in dc.ShopArtikels on artikelen.ArtikelID equals sa.ArtikelID
join at in dc.ArtikelTaals on artikelen.ArtikelID equals at.ArtikelID
join cat in dc.Categories on artikelen.CategorieID equals cat.CategorieID
join catt in dc.CategorieTaals on cat.CategorieID equals catt.CategorieID
join catg in dc.CategorieGroeps on cat.CategorieGroepID equals catg.CategorieGroepID
join catgt in dc.CategorieGroepTaals on catg.CategorieGroepID equals catgt.CategorieGroepID
join sh in dc.Shops on sa.ShopId equals sh.ShopID
join catgs in dc.CategorieGroepShops on sh.ShopID equals catgs.ShopId
join tl in dc.Taals on new { tc1 = at.TaalCode, tc2 = catgt.TaalCode } equals new { tc1 = tl.TaalCode, tc2 = tl.TaalCode } 
where sh.ShopID == shop.BLL.Business.ShopController.CurrentShop.Id
select dc.Artikels;

but I have the idea that I made some (minor) mistakes while joining. any ideas please!

EDIT I have rewritten the LINQ query to this:

var allProducts = from artikelen in dc.Artikels
join at in dc.ArtikelTaals on artikelen.ArtikelID equals at.ArtikelID
join sa in dc.ShopArtikels on artikelen.ArtikelID equals sa.ArtikelID
join cat in dc.Categories on artikelen.CategorieID equals cat.CategorieID
join catt in dc.CategorieTaals on cat.CategorieID equals catt.CategorieID
join sh in dc.Shops on sa.ShopId equals sh.ShopID
join catgs in dc.CategorieGroepShops on sh.ShopID equals catgs.ShopId
join tl in dc.Taals on new { tc1 = at.TaalCode, tc2 = catt.TaalCode } equals new { tc1 = tl.TaalCode, tc2 = tl.TaalCode } 
join catgt in dc.CategorieGroepTaals on new { tl.TaalCode, catgs.CategorieGroepId } equals new { catgt.TaalCode, catgt.CategorieGroepID }
join catg in dc.CategorieGroeps on new { cat.CategorieGroepID, catgt.CategorieGroepID, catgs.CategorieGroepId } equals new { catg.CategorieGroepID, catg.CategorieGroepID, catg.CategorieGroepID }
where sh.ShopID == 230
select dc.Artikels;

but I have a syntax error after "dut" } Edit 2: changed the join and replaced "dut" with the corresponding field in the database. still have the error after the first } it says: type inference failed in the call to 'Join'

+1  A: 

Some of the SQL joins have multiple join conditions, which you didn't put in the LINQ query.

Joachim VR
ok thanks, will look into that. haven't noticed it because my eyes are getting square shapes this friday
JP Hellemons
I have changed it now, but now I have a simple syntax error
JP Hellemons
What error, where?
Joachim VR
right after the `"dut" } `
JP Hellemons
I would only put database fields in the join condition. Other comparisons with supplied values (like "dut") should be placed in the where clause.
Joachim VR
@Joachim VR I've changed it now, but still a syntax error
JP Hellemons
A: 

There is a tool for it, but I didn't try it. May be it's usefull for you.

http://www.sqltolinq.com/

mavera
For straight forward queries like these that's probably a good idea, but eventually it's more useful to understand what kind of SQL LINQ generates. I've seen some weird stuff happening there.
Joachim VR
A: 

It looks like the error line is actually a "Where" cause but not "Joining". You can actually split the whole long Linq statement into smaller Query.

so for this case, its better to split it like this:

var at = from a in dc.ArtikelTaals
           where a.TaalCode == "dut"
           select a;

var catt = from c in dc.CategorieTaals
           where c.TaalCode == "dut"
           select c;

.....

and you can join the IQueryable "at" and "catt" in your complex query later.

Benedict
A: 

If this is something that will be frequently run then you should rewrite it as a stored procedure. I believe it is too convoluted and complex for a LINQ statement - too hard to see what's going on.

Kirk Broadhurst