0

I have the following tables:

Monster:  
Name     Description              EatsPeople 
Vampire  Pale, afraid of light    True 
Ghost    See-through, annoying    False 
Wraith   Green-ish, ugly, dumb    True 

TagLookup: 
Name    ID 
Ghost   1 
Ghost   2 
Wraith  1 

Tags: 
ID    Text                      Value 
1     Green                     green-skin 
2     Screams like a banshee    banshee-call 

To query a monster that has a green-skin tag I use this SQL query:

SELECT m.Name, m.Description, m.EatsPeople 
  FROM dbo.Monster AS m 
  INNER JOIN dbo.TagLookup AS tl 
    ON m.Name = tl.Name 
  INNER JOIN dbo.Tags AS t 
    ON t.ID = tl.ID 
    AND t.Value = 'green-skin'; 

This works fine and dandy as you'd expect but I'm having trouble with a LINQ version of this query. I've tried LinqPad with no luck + search Bing + Stackoverflow with not much luck

Alex Guerin
  • 2,336
  • 10
  • 36
  • 53

2 Answers2

1

This will result in the same join:

var result=(
    from m in db.Moster
    join tl in db.TagLookup
        on tl.Name equals tl.Name
    from t in db.Tags.Where(x=>x.ID==tl.ID && x.Value = "green-skin")
    select new
    {
        m.Name, 
        m.Description, 
        m.EatsPeople 
    });

Or you can do it like this as well. This will result in the same result:

var result=(
    from m in db.Moster
    join tl in db.TagLookup
        on tl.Name equals tl.Name
    join t in db.Tags
        on tl.ID equals t.ID
    where
        t.Value = "green-skin"
    select new
    {
        m.Name, 
        m.Description, 
        m.EatsPeople 
    });
Arion
  • 31,011
  • 10
  • 70
  • 88
0

try this:

 var a = from m in Monsters
            join tl in TagLookup  
                on m.Name equals tl.Name  
            join t in Tags
                on t.ID = tl.ID  
            where t.Value = 'green-skin';
            select new 
            {
                 m.Name, 
                 m.Description, 
                 m.EatsPeople  
            };
Arian
  • 12,793
  • 66
  • 176
  • 300