开发者

LINQ to SQL "complex" select

I have Country=>Ligue=>Team(Name, Score)

I need to select all team N开发者_Python百科ame-Scores from a country.

something like this, does not work )

query = from ligue in myCountry.Ligues, from team in ligue.Teams select name = team.Name, score = team.Score distinct

EDIT:

VB.NET syntax is preferable.


You should be able to do a simple Select / SelectMany

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


Here's the code from Kirk translated to VB10 extension method syntax:

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()

I believe (but am not sure, don't have access to a VB compiler right now) you can write it like this vb.net query syntax

(EDIT my original trial was indeed incorrect, so I corrected the query below:)

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


Using jeroenh's code, that used Kirk's code, here is the working version (VB.NET)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜