2011-09-05 83 views
0

我有国家=>法甲=>小组(姓名,分数)LINQ to SQL的“复杂”的选择

我需要从一个国家选择所有球队名称分数。

这样的事情,不工作)

查询= 法甲 myCountry.Ligues, ligue.Teams 选择名= team.Name,score = team.Score不同

编辑:

VB.NET语法是优选的。

+0

重新标记的问题,因为它原来并不被LINQ到SQL – jeroenh

+0

@ jeroenh我认为这个问题是关于LINQ到SQL的;对不起,我没有提供你喜欢的语法。 –

+0

@kirk如果你看看serhio的评论,我认为他把'linq to sql'误认为'linq查询语法'。可能是错误的。另外,我没有任何语法首选项,你的答案是完全有效的,我赞成它,我刚刚根据编辑的问题添加了VB.Net语法的答案。 – jeroenh

回答

1

使用jeroenh的代码,所用柯克的代码,这里是工作版本(VB.NET)

Dim query = From ligue In myCountry.Ligues 
       From team In ligue.Teams 
       Select Name = team.Name, Score = team.Score 
       Distinct 
4

你应该能够做一个简单的选择/的SelectMany

context.Countries.Single(c => c.CountryName == "My Country") 
    .Ligues.SelectMany(ligue => ligue.Teams 
     .Select(team => new { team.Name, team.Score })) 
     .Distinct(); 
+0

谢谢...说,这不是LINQ to SQL,但我会尝试这样),这不是独特的... – serhio

+1

@serhio我已经添加了一个'Distinct'。这**是** LINQ到SQL;只是它使用“方法”语法而不是“查询”语法。 –

+0

这是Linq,但不是Linq-to-SQL,因为您不使用任何“SQL”语法。 – serhio

3

下面是柯克的代码转换为VB10扩展方法的语法:

dim result = context.Countries.Single(Function(c) c.CountryName = "My Country"). 
       Ligues.SelectMany(Function(ligue) ligue.Teams). 
         Select(Function(team) new with {team.Name, team.Score }). 
         Distinct() 

我相信,(但我不知道,唐现在无法访问VB编译器)你可以这样写它vb.net查询语法

(编辑我原来的试验确实是不正确的,所以我纠正了下面的查询:)

dim result = From ligue in myCountry.Ligues 
      From team in ligue.Teams 
      Select team.Name, team.Score Distinct