我有国家=>法甲=>小组(姓名,分数)LINQ to SQL的“复杂”的选择
我需要从一个国家选择所有球队名称分数。
这样的事情,不工作)
查询= 从法甲在 myCountry.Ligues,从队在 ligue.Teams 选择名= team.Name,score = team.Score不同
编辑:
VB.NET语法是优选的。
我有国家=>法甲=>小组(姓名,分数)LINQ to SQL的“复杂”的选择
我需要从一个国家选择所有球队名称分数。
这样的事情,不工作)
查询= 从法甲在 myCountry.Ligues,从队在 ligue.Teams 选择名= team.Name,score = team.Score不同
编辑:
VB.NET语法是优选的。
使用jeroenh的代码,所用柯克的代码,这里是工作版本(VB.NET)
Dim query = From ligue In myCountry.Ligues
From team In ligue.Teams
Select Name = team.Name, Score = team.Score
Distinct
你应该能够做一个简单的选择/的SelectMany
context.Countries.Single(c => c.CountryName == "My Country")
.Ligues.SelectMany(ligue => ligue.Teams
.Select(team => new { team.Name, team.Score }))
.Distinct();
下面是柯克的代码转换为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
重新标记的问题,因为它原来并不被LINQ到SQL – jeroenh
@ jeroenh我认为这个问题是关于LINQ到SQL的;对不起,我没有提供你喜欢的语法。 –
@kirk如果你看看serhio的评论,我认为他把'linq to sql'误认为'linq查询语法'。可能是错误的。另外,我没有任何语法首选项,你的答案是完全有效的,我赞成它,我刚刚根据编辑的问题添加了VB.Net语法的答案。 – jeroenh