0

我有以下表格:

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 

要查询具有绿色皮肤标签的怪物,我使用以下 SQL 查询:

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'; 

正如您所期望的那样,这工作得很好而且很花哨,但是我在使用这个查询的 LINQ 版本时遇到了问题。我试过 LinqPad 没有运气 + 搜索 Bing + Stackoverflow 运气不太好

4

2 回答 2

1

这将导致相同的结果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 
    });

或者你也可以这样做。这将导致相同的结果:

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 
    });
于 2012-04-19T06:35:01.753 回答
0

试试这个:

 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  
            };
于 2012-04-19T06:35:35.090 回答