Spr*_*tar 0 c# sql linq linq-to-sql entity-framework-4.1
我有以下型号:

我需要一个特定用户的所有灯具和预测列表(如果该灯具有预测).sql返回我需要的内容如下:
SELECT * FROM Fixture f
LEFT OUTER JOIN Prediction p ON f.FixtureId = p.FixtureId
WHERE p.UserID = '06E4D3E0-8365-45BF-9054-3F8534C7AD5E' OR p.UserID IS NULL
Run Code Online (Sandbox Code Playgroud)
我试过了:
var query = from f in c.Fixtures
from p in c.Predictions.Where(pre => pre.UserId == new Guid("06E4D3E0-8365-45BF-9054-3F8534C7AD5E") || pre.UserId == null)
select new
{
FixtureId = f.FixtureId,
HomeScore = f.HomeTeamScore,
AwayScore = f.AwayTeamScore,
PredictionId = p.PredictionId,
HomePrediction = p.HomeTeamPrediction,
AwayPrediction = p.AwayTeamPrediction
};
Run Code Online (Sandbox Code Playgroud)
但这会产生(并给出错误的结果):
SELECT
[Extent1].[FixtureId] AS [FixtureId],
[Extent1].[HomeTeamScore] AS [HomeTeamScore],
[Extent1].[AwayTeamScore] AS [AwayTeamScore],
[Extent2].[PredictionId] AS [PredictionId],
[Extent2].[HomeTeamPrediction] AS [HomeTeamPrediction],
[Extent2].[AwayTeamPrediction] AS [AwayTeamPrediction]
FROM [dbo].[Fixture] AS [Extent1]
CROSS JOIN [dbo].[Prediction] AS [Extent2]
WHERE cast('06e4d3e0-8365-45bf-9054-3f8534c7ad5e' as uniqueidentifier) = [Extent2].[UserId]
Run Code Online (Sandbox Code Playgroud)
添加DefaultIfEmpty到第二个'from'像:
var query = from f in c.Fixtures
from p in c.Predictions.Where(pre => pre.UserId == new Guid("06E4D3E0-8365-45BF-9054-3F8534C7AD5E") || pre.UserId == null).DefaultIfEmpty()
select new
{
FixtureId = f.FixtureId,
HomeScore = f.HomeTeamScore,
AwayScore = f.AwayTeamScore,
PredictionId = p.PredictionId,
HomePrediction = p.HomeTeamPrediction,
AwayPrediction = p.AwayTeamPrediction
};
Run Code Online (Sandbox Code Playgroud)
生成(并且仍然给出错误的结果):
SELECT
[Extent1].[FixtureId] AS [FixtureId],
[Extent1].[HomeTeamScore] AS [HomeTeamScore],
[Extent1].[AwayTeamScore] AS [AwayTeamScore],
[Join1].[PredictionId] AS [PredictionId],
[Join1].[HomeTeamPrediction] AS [HomeTeamPrediction],
[Join1].[AwayTeamPrediction] AS [AwayTeamPrediction]
FROM [dbo].[Fixture] AS [Extent1]
CROSS JOIN (SELECT [Project1].[PredictionId] AS [PredictionId], [Project1].[HomeTeamPrediction] AS [HomeTeamPrediction], [Project1].[AwayTeamPrediction] AS [AwayTeamPrediction]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN (SELECT
[Extent2].[PredictionId] AS [PredictionId],
[Extent2].[UserId] AS [UserId],
[Extent2].[HomeTeamPrediction] AS [HomeTeamPrediction],
[Extent2].[AwayTeamPrediction] AS [AwayTeamPrediction]
FROM [dbo].[Prediction] AS [Extent2]
WHERE cast('06e4d3e0-8365-45bf-9054-3f8534c7ad5e' as uniqueidentifier) = [Extent2].[UserId] ) AS [Project1] ON 1 = 1 ) AS [Join1]
Run Code Online (Sandbox Code Playgroud)
使用现有的关系就像(这是我出错的地方,见下面的答案):
var query = from f in c.Fixtures
from p in c.Predictions
where c.Predictions.Any(pre => pre.UserId == new Guid("06E4D3E0-8365-45BF-9054-3F8534C7AD5E") || pre.UserId == null)
select new
{
FixtureId = f.FixtureId,
HomeScore = f.HomeTeamScore,
AwayScore = f.AwayTeamScore,
PredictionId = p.PredictionId,
HomePrediction = p.HomeTeamPrediction,
AwayPrediction = p.AwayTeamPrediction
};
Run Code Online (Sandbox Code Playgroud)
产生:
SELECT
[Extent1].[FixtureId] AS [FixtureId],
[Extent1].[HomeTeamScore] AS [HomeTeamScore],
[Extent1].[AwayTeamScore] AS [AwayTeamScore],
[Extent2].[PredictionId] AS [PredictionId],
[Extent2].[HomeTeamPrediction] AS [HomeTeamPrediction],
[Extent2].[AwayTeamPrediction] AS [AwayTeamPrediction]
FROM [dbo].[Fixture] AS [Extent1]
CROSS JOIN [dbo].[Prediction] AS [Extent2]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Prediction] AS [Extent3]
WHERE cast('06e4d3e0-8365-45bf-9054-3f8534c7ad5e' as uniqueidentifier) = [Extent3].[UserId]
)
Run Code Online (Sandbox Code Playgroud)
如何生成我需要的查询?
EF已为您生成导航属性,因此请继续使用它们!
代替
var query =
from f in c.Fixtures
from p in c.Predictions
.Where(pre => pre.UserId == new Guid("06E4D3E0-8365-45BF-9054-3F8534C7AD5E")
|| pre.UserId == null)
Run Code Online (Sandbox Code Playgroud)
尝试
var query =
from f in c.Fixtures
from p in f.Predictions
.Where(pre => pre.UserId == new Guid("06E4D3E0-8365-45BF-9054-3F8534C7AD5E")
|| pre.UserId == null)
Run Code Online (Sandbox Code Playgroud)
请注意,在EF中使用join和DefaultIfEmpty经常是错误的.
| 归档时间: |
|
| 查看次数: |
3146 次 |
| 最近记录: |