Linq to sql 不断返回 null

发布于 2024-12-10 04:46:12 字数 1012 浏览 0 评论 0原文

我有以下 SQL 查询:

SELECT DISTINCT 
  Participant.BackgroundTrainingID, 
  Location.TrainingSite
FROM Registration, ProgramLocation, Participant, Program, Location
WHERE ProgramLocation.LocationID = Location.LocationID 
  AND ProgramLocation.ProgramID=Registration.ProgramID 
  AND Registration.ParticipantID=Participant.ParticipantId

我编写了以下 LINQ to SQL 来匹配上面的查询:

var trainingsiteinfo = (from c in db.ProgramLocations 
                        from n in db.Registrations 
                        from l in db.Participants  
                        from h in db.Locations 
                        where c.LocationID == h.LocationID 
                          && c.ProgramID == n.ProgramID 
                          && n.ParticipantID == l.ParticipantId 
                        select new { 
                          h.TrainingSite, 
                          l.BackgroundTrainingID }).Distinct();

SQL 查询工作正常,但 LINQ 不断返回 null。

I have the following SQL query:

SELECT DISTINCT 
  Participant.BackgroundTrainingID, 
  Location.TrainingSite
FROM Registration, ProgramLocation, Participant, Program, Location
WHERE ProgramLocation.LocationID = Location.LocationID 
  AND ProgramLocation.ProgramID=Registration.ProgramID 
  AND Registration.ParticipantID=Participant.ParticipantId

I wrote the following LINQ to SQL to match the query above:

var trainingsiteinfo = (from c in db.ProgramLocations 
                        from n in db.Registrations 
                        from l in db.Participants  
                        from h in db.Locations 
                        where c.LocationID == h.LocationID 
                          && c.ProgramID == n.ProgramID 
                          && n.ParticipantID == l.ParticipantId 
                        select new { 
                          h.TrainingSite, 
                          l.BackgroundTrainingID }).Distinct();

The SQL query works fine but LINQ constantly returns null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘦怹 2024-12-17 04:46:12

当我失去想法时,我使用 Linqer http://www.sqltolinq.com/ :-)加快转换作业。

I use Linqer http://www.sqltolinq.com/ when i get out of ideas :-) Aswell it speeds up conversion jobs.

旧瑾黎汐 2024-12-17 04:46:12

在绑定结果之前,请确保您的数据库上下文没有超出范围。您可以通过在 .Distinct() 之后添加 .ToList() 来强制提前加载结果来测试这一点。

Make sure your db context isn't going out of scope before you have bound your results. You can test this by adding .ToList() after your .Distinct() to force eager loading of the results.

只为一人 2024-12-17 04:46:12

您的 linq 代码在 select 语句中有一个未闭合的 } 括号

Your linq code has an unclosed } bracket in the select statement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文