我有國家=>法甲=>小組(姓名,分數)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