Sql server SQL查询的LINQ版本
我有以下表格:Sql server SQL查询的LINQ版本,sql-server,linq,entity-framework,linq-to-sql,tagging,Sql Server,Linq,Entity Framework,Linq To Sql,Tagging,我有以下表格: 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:
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这将导致相同的
加入
:
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
});
试试这个:
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
};