Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多表Linq连接查询_Linq - Fatal编程技术网

多表Linq连接查询

多表Linq连接查询,linq,Linq,我需要的查询是: 我想列出所有的高尔夫球场(GolfCourseInfo)和日期(GolfQuestTee) 并不是所有的高尔夫球场都打过,所以DatePlayed将是空的,但我需要列出所有的高尔夫球场,不管是什么 我在LinqPad中遇到了一些查询,但它们总是只返回具有相应DatePlayed值的课程,这不是我需要的 非常感谢您的帮助。猜猜看,像这样的事情 from c in GolfCourseInfo join t in Tee on c.Id equals t.CourseInfoId

我需要的查询是:

我想列出所有的高尔夫球场(GolfCourseInfo)和日期(GolfQuestTee)

并不是所有的高尔夫球场都打过,所以DatePlayed将是空的,但我需要列出所有的高尔夫球场,不管是什么

我在LinqPad中遇到了一些查询,但它们总是只返回具有相应DatePlayed值的课程,这不是我需要的


非常感谢您的帮助。

猜猜看,像这样的事情

from c in GolfCourseInfo 
join t in Tee on c.Id equals t.CourseInfoId
join q in GolfQuestTee on t.Id equals q.TeeId
select new
{
   CourseID = c.Id,
   CourseName = c.CourseName,
   Location = c.Location,
   DatePlayed = q.DatePlayed
}

猜猜看,像这样吗

from c in GolfCourseInfo 
join t in Tee on c.Id equals t.CourseInfoId
join q in GolfQuestTee on t.Id equals q.TeeId
select new
{
   CourseID = c.Id,
   CourseName = c.CourseName,
   Location = c.Location,
   DatePlayed = q.DatePlayed
}

假设您只是连接到Linqpad中的数据库(因此,在后台使用LINQ to SQL),应该是这样的:

from info in GolfCourseInfos
select new { 
               info,
               Dates = info.Tees.SelectMany(t => t.GolfQuestTees)
                                .Select(x => x.DatePlayed)
           }

它将提供空的
日期
集合,其中没有日期。

假设您只连接到Linqpad中的数据库(因此,在引擎盖下使用LINQ to SQL),它应该是这样的:

from info in GolfCourseInfos
select new { 
               info,
               Dates = info.Tees.SelectMany(t => t.GolfQuestTees)
                                .Select(x => x.DatePlayed)
           }

它将提供空的
日期
集合,其中没有日期。

您要查找的是执行多个左外联接。看看。@TylerOhlsen-我看了这个答案,虽然这个概念可能有效,但我无法完全实现它。它正在返回重复的GolfCourseInfo课程名称。我想不出将Distinct运算符放在何处。您要查找的是执行多个左外联接。看看。@TylerOhlsen-我看了这个答案,虽然这个概念可能有效,但我无法完全实现它。它正在返回重复的GolfCourseInfo课程名称。我无法确定将Distinct运算符放在何处。这与我的尝试几乎相同,但遗憾的是没有返回正确的结果。它只返回已经玩过的高尔夫球场,不返回还没有玩过的。这与我的尝试几乎相同,但不幸的是没有返回正确的结果。它只返回那些已经玩过的高尔夫球场,而不返回那些还没有玩过的。这很有效!我没想到它会如此简洁、简单(看起来)。谢谢。我正在尝试将日期集合更改为viewmodel的字符串。DatePlayed属性将为空或格式化的日期字符串。我很难让它发挥作用。我得到一个错误,.ToString()或.ToSortDateString()无法识别。你知道我如何返回日期字符串吗?没关系。我想出来了。我创建了两个运行,第一个运行返回您的查询,然后再次运行,并将类型转换为我的viewmodel。谢谢,这很有效!我没想到它会如此简洁、简单(看起来)。谢谢。我正在尝试将日期集合更改为viewmodel的字符串。DatePlayed属性将为空或格式化的日期字符串。我很难让它发挥作用。我得到一个错误,.ToString()或.ToSortDateString()无法识别。你知道我如何返回日期字符串吗?没关系。我想出来了。我创建了两个运行,第一个运行返回您的查询,然后再次运行,并将类型转换为我的viewmodel。谢谢